Replace auto-generated OBJECTID with my own ID field when creating a GeoJSON layer

1392
5
11-09-2021 05:03 PM
ironmaskito
New Contributor III

I am creating a GeoJSONLayer client-side using an example payload containing geojson data. ArcGIS auto-generates a `OBJECTID` attribute, and I'm hoping I can replace this value with an `id` feature property in my payload. My goal is to query for the feature using the id provided in my geojson data. I think GeoJSONLayer's objectIdField will do the trick, but I can't figure out how to implement it based off of the accompanying direction:

The name of an oid field containing a unique value or identifier for each feature in the layer. id property of the feature object in the GeoJSON will be used as ObjectID. If id property is not present and objectIDField is not specified, ObjectID field will be generated for each feature.

I've included a snippet of Geojson data I want to display below with the `id` property (0x1023) providing the new value for `ObjectID`.

 

 

{
  "overlayId": "2d882141-0d9e-59d4-20bb-58e6d0460699.1",
  "featureId": "example.geojson.1",
  "format": "geojson",
  "feature": {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [
            -15.9423828125,
            -15.297068292853805
          ]
        },
        "properties": {
          "sys_style": "default"
        },
        "name": "Point",
        "id": "0x1023",
        "description": "Feature Point Example"
      }
	]
	}
}

 

 

 

However, if I create a GeoJSONLayer from the above data, ArcGIS simple autogenerates the ObjectID like usual (first feature has ObjectID = 1, and so on). I also tried setting GeoJSONLayer's `objectIdField` to `id`, but that didn't work either. I'm a bit stuck, as I couldn't find any examples online trying to accomplish what I am. Would appreciate any help.

 

 

    this.layer = new GeoJSONLayer({
      url: createUrl(geojson),
      renderer: this.getRenderer(), 
      elevationInfo: {
        mode: 'on-the-ground'
      },
      objectIdField: "id", // use the id in geojson data instead of the auto-generated OBJECTID as an attribute
    });
  }

  private createUrl(geojson: GeoJSON.GeoJSON): string {
    const blob: Blob = new Blob([JSON.stringify(geojson)], {
      type: 'application/json'
    });
    return URL.createObjectURL(blob);
  }

 

 

 

0 Kudos
5 Replies
shaylavi
Esri Contributor

When working with ESRI's API and maps, ObjectID is required as part of the ability to process the layer object. If you'd like to have a customized structure why use the API and not built one manually through a function that adds the details into a GeoJSON format?

 

Shay
0 Kudos
ironmaskito
New Contributor III

The use case above is for testing purposes. I just want to know if what I'm trying to accomplish is possible with objectIdField property.

0 Kudos
ironmaskito
New Contributor III

I would also be open to adding my id field as an additional attribute to the geojson feature. I can't figure out how to add my own custom attributes, so I'm stuck with the auto-generated OBJECTID. I know ideally I would be setting/getting this information from a database, but for now I'm forced to store data on my local machine.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

Until versions up to 4.21, The GeoJSONLayer expects the id property of the feature object in the GeoJSON to be of type number. Other data types are not honored. It is stated in known limitations section of the GeoJSONLayer document.

However, starting at version 4.22, GeoJSONLayer  supports string and number feature ids for the ObjectID field. You can test this using our next build which shows what's coming at 4.22. 

This simple test app shows that this can now be done at the JS API version 4.22. The version 4.22 will be released end of December. 

 

Hope this helps,

-Undral

 

0 Kudos
ironmaskito
New Contributor III

I've figured out a workaround. I need to add the property outFields: ["*"] to GeoJSONLayer to include my id as an attribute.

0 Kudos