-
-
Notifications
You must be signed in to change notification settings - Fork 47
ArcGIS Queries
Kehang Han edited this page Aug 21, 2019
·
4 revisions
TODO
https://esri.github.io/arcgis-python-api/apidoc/html/index.html
These helper functions are available at the gis_utils.py
file.
from arcgis.gis import *
from arcgis.features import FeatureLayer
from arcgis import geometry
from pprint import pprint # for printing JSON nicely
gis = GIS()
URL = "https://services.arcgis.com/sFnw0xNflSi8J0uh/ArcGIS/rest/services/Supermarkets_GroceryStores/FeatureServer/0/"
QUERY = { 'where': '1=1', 'out_sr': '4326' }
def get_features_from_feature_server(url, query):
"""
Given a url to a City of Boston Feature Server, return a list
of Features (for example, parking lots that are not full)
:param url: url for Feature Server
:param query: query to select features (example: "Spaces > 0")
:return: list of all features returned from the query
"""
features = []
f = FeatureLayer(url = url)
feature_set = f.query(**query)
for feature in feature_set:
features.append(feature.as_dict)
return features
grocery_stores = get_features_from_feature_server(URL, QUERY)
pprint(grocery_stores[0])
Translating and address such as 220 Clarendon Street
to a long, lat
JSON object {'x': -71.07545238040102, 'y': 42.3509583718135}
from arcgis.gis import *
from arcgis.geocoding import geocode
gis = GIS()
def geocode_address(m_address):
"""
:param m_address: address of interest in street form
:return: address in coordinate (X and Y) form
"""
m_address = m_address + ", City: Boston, State: MA"
m_location = geocode(address=m_address)[0]
return m_location['location']
from arcgis.gis import *
from arcgis import geometry
gis = GIS()
FEATURE1 = grocery_stores[0] # from helper function above
FEATURE2 = grocery_stores[1]
def get_geodesic_distance(feature1, feature2):
geometry1 = feature1['geometry'] # if feature1 is an address, use just feature1 (not `['geometry']`)
geometry2 = feature2['geometry']
spation_ref = {"wkid" : 4326}
return geometry.distance(spation_ref,
geometry1,
geometry2,
distance_unit='',
geodesic=True)['distance']
distance = get_geodesic_distance(FEATURE1, FEATURE2)
print(distance) # in meters
MILE = 1600 # meters
DAY = datetime.datetime.today().strftime('%A')
food_trucks_url = 'https://services.arcgis.com/sFnw0xNflSi8J0uh/arcgis/rest/services/food_trucks_schedule/FeatureServer/0'
address = geocode_address('120 Boylston St')
# This will return a list of all available food trucks in Boston
trucks = get_feature_location(food_trucks_url, QUERY)
# Filter features by attributes (in this case, Day = Today and Time = Lunch)
trucks_today = []
for t in trucks:
if(t['attributes']['Day'] == DAY and t['attributes']['Time'] == 'Lunch'):
trucks_today.append(t)
# Print only features that are within a mile
for t in trucks_today:
distance = get_geodesic_distance(address, t)
if distance <= MILE:
print(t['attributes']['Truck'] + ' is located at ' + \
t['attributes']['Loc'] + ' between ' + \
t['attributes']['Start_time'] + ' and ' + \
t['attributes']['End_time'] + '\n')