Using geometry functions

This notebook uses the arcgis.geometry module to compute the length of a path that the user draws on the map.

The particular scenario is of a jogger who runs in the Central Park in New York (without gizmos like GPS watches to distract him), and wants a rough estimate of his daily runs based on the path he takes. The notebook starts out with a satellite map of Central Park in New York:

from arcgis.gis import GIS
from arcgis.geocoding import geocode
from arcgis.geometry import lengths
gis = GIS()
map1 = gis.map()
map1.basemap = "satellite"
map1
map1.height = '650px'
location = geocode("Central Park, New York")[0]
map1.extent = location['extent']
map1.zoom = 14

We want the user to draw a freehand polyline to indicate the paths that he takes for his runs. When the drawing operation ends, we use the GIS's Geometry service to compute the length of the drawn path. We can do this by adding an event listener to the map widget that gets called when drawing is completed (i.e. on_draw_end). The event listener then computes the geodesic length of the drawn geometry using the geometry service and prints it out:

# Define the callback function that computes the length.
def calc_dist(map1, g):
    print("Computing length of drawn polyline...")
    length = lengths(g['spatialReference'], [g], "", "geodesic")
    print("Length: " + str(length[0]) + " m.")

# Set calc_dist as the callback function to be invoked when a polyline is drawn on the map
map1.on_draw_end(calc_dist)
map1.draw("polyline")

Now draw a polygon on the map representing the route taken by the jogger

map1.clear_graphics()

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.