Skip to main content
Version: 0.10.1

Visualizing Customer addresses on a flood plane

King County (WA) publishes flood plain data as well as tax parcel data. We can use the addresses in the tax parcel data and use the geocoder to calculate coordinates. Using this coordinates and the flood plain data we can enrich out dataset with a flag indicating whether the house is in a flood zone or not.

The following data has been sourced from King County's Open data portal. Link

  1. Address Data
  2. Flood plains

For this demonstration, please follow the instructions on setting up your azure maps account from the overview notebook.


  1. Upload the flood plains data as map data to your creator resource
import os
import json
import time
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

# Configure more resiliant requests to stop flakiness
retry_strategy = Retry(
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"],
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

from pyspark.sql import SparkSession
from import running_on_synapse, find_secret

# Bootstrap Spark Session
spark = SparkSession.builder.getOrCreate()

if running_on_synapse():
from notebookutils.visualization import display
# Azure Maps account key
maps_key = find_secret("azuremaps-api-key") # Replace this with your azure maps key

# Creator Geo prefix
# for this example, assuming that the creator resource is created in `EAST US 2`.
atlas_geo_prefix = "us"

# Load flood plains data
flood_plain_geojson = http.get(

# Upload this flood plains data to your maps/creator account. This is a Long-Running async operation and takes approximately 15~30 seconds to complete
r =

# Poll for resource upload completion
resource_location = r.headers.get("location")
for _ in range(20):
resource = json.loads(
status = resource["status"].lower()
if status == "running":
time.sleep(5) # wait in a polling loop
elif status == "succeeded":
raise ValueError("Unknown status {}".format(status))

# Once the above operation returns a HTTP 201, get the user_data_id of the flood plains data, you uploaded to your map account.
user_data_id_resource_url = resource["resourceLocation"]
user_data_id = json.loads(

Now that we have the flood plains data setup in our maps account, we can use the CheckPointInPolygon function to check if a location (lat,lon) coordinate is in a flood zone.

Load address data:

data ="header", "true").csv(

# Visualize incoming schema

# Choose a subset of the data for this example
subset_data = data.limit(50)

Wire-up the Address Geocoder

We will use the address geocoder to enrich the dataset with location coordinates of the addresses.

from pyspark.sql.functions import col
from import *
from import FixedMiniBatchTransformer, FlattenBatch
from import *

def extract_location_fields(df):
# Use this function to select only lat/lon columns into the dataframe

# Azure Maps geocoder to enhance the dataframe with location data
geocoder = (

# Set up a fixed mini batch transformer to geocode addresses
batched_dataframe = geocoder.transform(
geocoded_addresses = extract_location_fields(

# Display the results

Now that we have geocoded the addresses, we can now use the CheckPointInPolygon function to check if a property is in a flood zone or not.

Setup Check Point In Polygon

def extract_point_in_polygon_result_fields(df):
# Use this function to select only lat/lon columns into the dataframe
col("output.result.pointInPolygons").alias("In Polygon"),
col("output.result.intersectingGeometries").alias("Intersecting Polygons"),

check_point_in_polygon = (

flood_plain_addresses = extract_point_in_polygon_result_fields(

# Display the results

Cleanup Uploaded User Data (Optional)

You can (optionally) delete the uploaded geojson polygon.

res = http.delete(