Skip to content

Instantly share code, notes, and snippets.

@nmpeterson
Last active January 3, 2020 18:19
Show Gist options
  • Save nmpeterson/9764120 to your computer and use it in GitHub Desktop.
Save nmpeterson/9764120 to your computer and use it in GitHub Desktop.
An arcpy-based function for creating a point feature class of all Divvy bike share (https://divvybikes.com) stations from their current JSON data, for use within ArcMap/ArcGIS.
import arcpy
import json
import os
import sys
if ((3, 0) <= sys.version_info <= (3, 9)):
from urllib.request import urlopen # Python 3
elif ((2, 0) <= sys.version_info <= (2, 9)):
from urllib2 import urlopen # Python 2
def create_divvy_points_from_json(out_fc):
''' Opens a URL containing Divvy station data in JSON format and uses the
"latitude" and "longitude" attributes to create a point shapefile/
feature class. '''
JSON_URL = 'https://gbfs.divvybikes.com/gbfs/en/station_information.json'
SR_WGS84 = arcpy.SpatialReference(4326) # lat-lon projection for Divvy coordinates
arcpy.CreateFeatureclass_management(os.path.dirname(out_fc), os.path.basename(out_fc), 'POINT', spatial_reference=SR_WGS84)
arcpy.AddField_management(out_fc, 'STATION_ID', 'LONG')
arcpy.AddField_management(out_fc, 'NAME', 'TEXT', field_length=140)
arcpy.AddField_management(out_fc, 'DOCKS', 'SHORT')
arcpy.AddField_management(out_fc, 'LATITUDE', 'DOUBLE')
arcpy.AddField_management(out_fc, 'LONGITUDE', 'DOUBLE')
with arcpy.da.InsertCursor(out_fc, ['SHAPE@XY', 'STATION_ID', 'NAME', 'DOCKS', 'LATITUDE', 'LONGITUDE']) as c:
json_file = urlopen(JSON_URL)
divvy_json = json.load(json_file)
for station in divvy_json['data']['stations']:
station_id = int(station['station_id'])
name = station['name']
docks = station['capacity']
latitude = station['lat']
longitude = station['lon']
xy = (longitude, latitude)
row = [xy, station_id, name, docks, latitude, longitude]
c.insertRow(row)
json_file.close()
return out_fc
divvy_stations_fc = create_divvy_points_from_json(r'C:\Users\npeterson\Documents\ArcGIS\Default.gdb\divvy_stations')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment