2

I'm working in Jupyter Notebook and attempting to symbolize an external ESRI GeoJSON service (now an output available from ArcServer 10.5) by an attribute of a local GeoJSON, and return a choropleth map. The premise is one of combining customer data with a publicly available service.

If the two GeoJSON files are both local, I have no problem loading them with geopandas and achieving the desired result on a folium map. However the utilization of an ESRI GeoJSON service url is proving difficult.

Anyone know of a way to do this with an external GeoJSON?

Am I missing a transformation of the ESRI GeoJSON output?

Code:

    import geopandas as gpd
    import folium
    import requests
    import json

    mymap = folium.map([42.65, -73.75], 
        zoom_start=7,
        tiles = 'cartodbpositron')

    geo = gpd.read_file('local.geojson')

    url = json.loads(requests.get('https://gisservices:domain/arcgis/rest/services/...f=geojson').text)

    mymap = choropleth(geo_path = url,
            fill_color='YlGn', fill_opacity=0.5, line_opacity=0.5,
            threshold_scale = [50,100,250,500,750,900],
            data = geo,  #data source I want to pull the attribute from
            key_on='feature.properties.NAME', #key field from external GeoJSON
            columns = ['COUNTY', 'Value']  #key field from local GeoJSON followed by the field I want to symbolize by                               
            )

Examining the data and the request I get a 200 response and what looks like a properly formatted geojson.

    {'crs': {'properties': {'name': 'EPSG:4326'}, 'type': 'name'},
        'features': [{'geometry': {'coordinates': [[[-75.88982572053291,
             42.723834549294345],
             [-75.88998066913965, 42.725786369710484],
             [-75.89004324847727, 42.72657462151952],
             [-75.8900956058853, 42.72723409146341],
             ...

However the final portion of the code returns an error that the geo_path for the folium choropleth contains an invalid file: "TypeError: invalid file: {'type': 'FeatureCollection', 'crs':... "

1

Updating my previous post I think I found an answer. In the code above I used "geo_path" when building the map choropleth. In the error output I missed an important distinction when folium creates the GeoJSON object:

if geo_path:
    geo_data = open(geo_path)
elif geo_str:
    geo_data = geo_str

Therefore I modified the code to instead use "geo_str" and was able to get a working map after export to html. Funny thing is that the map doesn't display inside the Jupyter notebook but looks fine when I open the html file.

Working Code:

import geopandas as gpd
import folium
import requests
import json

mymap = folium.map([42.65, -73.75], 
    zoom_start=7,
    tiles = 'cartodbpositron')

geo = gpd.read_file('local.geojson')

url = 'https://gisservices:domain/arcgis/rest/services/...f=geojson'

data = requests.get(url)
data_json = r.json()

mymap = choropleth(geo_data = data_json,
        fill_color='YlGn', fill_opacity=0.5, line_opacity=0.5,
        threshold_scale = [50,100,250,500,750,900],
        data = geo,  #data source I want to pull the attribute from
        key_on='feature.properties.NAME', #key field from external GeoJSON
        columns = ['COUNTY', 'Value']  #key field from local GeoJSON followed by the field I want to symbolize by                               
        )

mymap.save(outfile='test.html')

The code generates a html map that symbolizes county polygons from an Arc Server 10.5 feature service by a value contained within a local GeoJSON.

  • Commenting on the Folium version update, now using 0.5.0, the "geo_str" parameter has been changed and the code now uses "geo_data". – ScottG Aug 17 '18 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.