question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting from GeoJson to ESRI Feature Set causes malformed ring data

See original GitHub issue

Using Python API 1.7.1 I have attempted to use the feature layer method edit_features() with a set of features that have been converted from GeoJson to an ESRI Feature Set. In doing so this error message was returned:

The specified geometry is not in the correct format. Object reference not set to an instance of an object.

While looking at the converted ESRI Feature Set, the ring data had nulls inserted into the data. Upon closer inspection of the from_geojson() FeatureSet method in /arcgis/featues/featury.py a few lines caught my attention.

# Lines 873-896
elif geo_type == "MultiPolygon":
    rings = []
    if HASARCPY:
        geom = arcpy.AsShape(geom)
        geometry = Geometry(json.loads(geom))
    else:
        coordkey = ([d for d in geom if d.lower() == 'coordinates']
                        or ['coordinates']).pop()
        coordinates = geom[coordkey]
        typekey = ([d for d in geom if d.lower() == 'type']
                        or ['type']).pop()
        if geom[typekey].lower() == "polygon":
            coordinates = [coordinates]
        part_list = []
        for part in coordinates:
            part_item = []
            for idx, ring in enumerate(part):
                if idx:
                    part_item.append(None)
                for coord in ring:
                    part_item.append(coord)
            if part_item:
                part_list.append([part_item])
        geometry["rings"] = part_list[0]

This seems to append a None value to the beginning of every new ring set. I’m not sure whether the reasoning for this is for internal api usage. However, when using this method within my context the rings with null entries will not be accepted by the edit_features() method. Instead, I would be expecting it to be placed within an array for the relevant ring geometries, and for the set of rings to also be placed in an array

Below I have provided code I used to test this issue.

import json
from arcgis.gis import GIS, User, ContentManager
from arcgis.features import FeatureSet

gis = GIS("https://www.arcgis.com", username="Username", password="******")
user = User(gis, 'Folder')
content = ContentManager(gis)

poly_service = content.search('title:poly_title AND type:Feature Service')
feature_item = poly_service[0]   
feature_layers = feature_item.layers
feature_set = feature_layers[0].query()
vem_flayer = feature_layers[0]

# Corrected ESRI-json rings without nulls
with open('corrected_esri_test.json') as infile:
    test = json.load(infile)
    esri_json = FeatureSet(test['features'])
    edit = vem_flayer.edit_features(adds=esri_json)
    print(edit)

# ESRI-json generated with from_geojson() method
with open('poly_test.json') as infile:
    test = json.load(infile)
    esri_json = FeatureSet.from_geojson(test)
    with open('esri_test.json','w') as outfile:
        outfile.write(str(esri_json))
        outfile.close()
        edit = vem_flayer.edit_features(adds=esri_json)
        print(edit)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jozsef-kepescommented, Feb 26, 2020

ArcPy is not being used and the python API is being run external of Pro. We are trying to run this on a AWS Lambda function. Looking at /arcgis/featues/feature.py provides some information as to why this might be a bug. Separate logic is being executed when ArcPy is not being used. We also noted the logic when its not using ArcPy only seems to take the first ring from a multi-polygon and leaves the rest, as well as adding nulls to separate inner rings.

The arcgis library was installed by running: pip install arcgis --no-deps by following the instructions found here

The json files used have been included below. json-files.zip

0reactions
nfink-nltcommented, Aug 3, 2021

I’m still seeing this issue in releases 1.8 and 1.9 where the conversion of a multipolygon to feature set will cause malformed data because a null value is being add for each ring in features\feature.py at lines 1058-1060: for idx, ring in enumerate(part): if idx: part_item.append(None)

This will always append a null value to the list because the index will always be True with enumerate. Commenting out the code that add the null value to the list fixes the error. This bug currently keeps me from uploading the data to ArcGIS Online with the error “The specified geometry is not in the correct format. Object reference not set to an instance of an object.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

FeatureSet.from_geojson bug? - Esri Community
The problem for me occurs when my geojson encodes a complex doughnut polygon and the ArcGIS API for Python appears to be attempting...
Read more >
GeoJson to FeatureLayer using Terraformer - Esri Community
I'm trying to convert a geoJSON to ESRI JSON (ultimately to create a FeatureLayer for use in a web map). I'm using the...
Read more >
Error converting Json file to a feature class - Esri Community
Solved: I want to convert some json data being returned from a request to a feature class, but I keep getting a RuntimeError:...
Read more >
Solved: Converting geometries between GeoJSON, esri JSON
I want to be able to convert geometries between GeoJSON [1], esri JSON [2, 3], and esri Python objects [4] using ArcGIS 10.1...
Read more >
ArcGIS JSON to GeoJSON does not always work
I'm reading ArcGIS Server responses with conversion to GeoJSON format as described in.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found