Read geometry from JSON API response in notebook

995
2
02-23-2023 06:10 AM
A_Schwab
New Contributor III

I'm trying to create a layer that updates with data from the IPC api (sample response below).

With GeoPandas, I can successfully load this into a geodataframe in one line:

 

gpd.GeoDataFrame.from_features(json['features'])

 

I've not been able to get a similar results using the ESRI notebook environment without geopandas.

I can create a data frame that brings through the attribute data:

 

df = pd.DataFrame()
for feature in json['features']: 
    temp_df = pd.DataFrame.from_dict(data=feature['properties'], orient='index').T
    geom = pd.DataFrame.from_dict(data=feature['geometry'], orient='index').T
    temp_df['geom_type'] = geom.type
    temp_df['geom'] = geom.coordinates    
    df = df.append(temp_df)

 

But if I try to wrap geom.coordinates in arcgis.geometry.Geometry() it gives an error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any help on how to correctly parse the JSON response attributes and geometry into something I can publish as a feature layer would be much appreciated.

 

sample API response:

 

{'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {'id': '24755676',
    'estimated_population': 363930,
    'period': 'C',
    'from': 'Jan 2022',
    'to': 'Jan 2022',
    'color': '#E67800',
    'anl_id': '24755659',
    'title': 'Middle Juba',
    'overall_phase': 3,
    'country': 'SO',
    'year': 2022,
    'condition': 'A',
    'phase3_worse_population': 0,
    'phase3_worse_percentage': 0,
    'phase1_population': 198570,
    'phase1_percent': 0.54,
    'phase1_color': '#CDFACD',
    'phase2_population': 85470,
    'phase2_percent': 0.23,
    'phase2_color': '#FAE61E',
    'phase3_population': 64190,
    'phase3_percent': 0.17,
    'phase3_color': '#E67800',
    'phase4_population': 15700,
    'phase4_percent': 0.04,
    'phase4_color': '#C80000',
    'phase5_population': 0,
    'phase5_percent': 0,
    'phase5_color': '#640000'},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[42.920335297, 0.31870463],
      [42.730911255, 0.311817259],
      [42.630912781, 0.721780658],
      [42.430908203, 1.321726918],
      [42.200901031, 1.311727881],
      [41.420883179, 1.261732697],
      [41.640888214, 1.941671729],
      [42.400909424, 1.951670408],
      [42.520908356, 1.881676555],
      [42.900917053, 1.521708727],
      [42.980918884, 1.461714149],
      [43.270923615, 1.031752586],
      [43.350925446, 0.921762526],
      [43.520351409, 0.676131546],
      [43.497226716, 0.652937771],
      [43.487045289, 0.645697118],
      [43.434757233, 0.589724184],
      [43.401786803, 0.56085235],
      [43.377445223, 0.535068869],
      [43.29442215, 0.436700226],
      [43.258571626, 0.401362718],
      [43.241474152, 0.381407499],
      [43.208774567, 0.350011586],
      [43.190654755, 0.328533321],
      [42.920335297, 0.31870463]]]} }]}

 

 

Tags (3)
0 Kudos
2 Replies
EarlMedina
Esri Regular Contributor

Hi,

Using the sample you provided, I was able to parse the JSON correctly into an SEDF like so:

import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor

parsed_json = [
    {
        **feature["properties"], 
        "geom": {
                    "rings" : feature["geometry"]["coordinates"],
                    "spatialReference" : {"wkid" : 4326}
                }
    } for feature in json["features"]]

df = pd.DataFrame.from_dict(parsed_json)
sedf = pd.DataFrame.spatial.from_df(df, geometry_column="geom")

 

Hope this helps!

 

0 Kudos
A_Schwab
New Contributor III

Thanks for taking time to look at this. 

The code you shared works for me to create a DF and then SEDF of the JSON. 

But when I try to visualise the data in the map widget with

sedf.spatial.plot(map_widget= map1)

 it throws an error (error text attached as widget_error.txt). 

I also tried using the SEDF to create a feature layer with

sedf.spatial.to_featurelayer('test', gis=GIS("home"))

this also threw an error, attached as to_fl_error.txt

If you have any insight into what might be causing the errors or how to proceed, it would be much appreciated. 

FWIW, if I access the geometry of a feature/row in the SEDF through `sedf[:1].geom[0]` the notebook correctly displays the geometry of the district. 

0 Kudos