Skip to main content
Question

Add ID column to GeoJSON file


Hi!

 

I´m trying to add an ID column to a GeoJSON file. I use a reader to read data from an Oracle database and then write it down using to GeoJSON writer.

 

I need the ID not to be in the properties-section but as in the example below:

https://community.safe.com/s/article/tutorial-getting-started-with-json

 

Thankful for any suggestions on how to do this 😊

9 replies

redgeographics
Celebrity
Forum|alt.badge.img+50

What you can do is insert a Counter transformer before the writer, it will simply number each feature coming through with an incremental number and you can write that to an attribute. Don't forget to add the attribute to your GeoJSON writer though! (in the User Attributes tab of the writer feature type properties).


  • Author
  • May 3, 2021

Thank you for the answer. It seems though that the attribute is added to the properties of the feature and not to the feature itself as in the example.


redgeographics
Celebrity
Forum|alt.badge.img+50

Would it be possible to share a small example of what you currently have and what you want to have?


  • Author
  • May 3, 2021
redgeographics wrote:

Would it be possible to share a small example of what you currently have and what you want to have?

I have uploaded a sample GeoJSON😊. I would like the ID-attribute to be at the features-level.


redgeographics
Celebrity
Forum|alt.badge.img+50
eriblan wrote:

I have uploaded a sample GeoJSON😊. I would like the ID-attribute to be at the features-level.

Ah, I see. I think you can't do that under the GeoJSON specs, from what I've been able to find out you can only have type, geometry and properties at the features level. The FME writer will add all attributes at the properties level.


maisels
Contributor
Forum|alt.badge.img+3
  • Contributor
  • May 27, 2021

Hi, I am new to this topic and have exactly the same problem. I would be interested to know if you have found a solution in the meantime? 


  • Author
  • May 31, 2021
maisels wrote:

Hi, I am new to this topic and have exactly the same problem. I would be interested to know if you have found a solution in the meantime? 

Hi, I didnt manage to add the id where I wanted it. I ended up opening the geojson file in QGIS and exported it with an id that way, although this way it is difficult to automate.


maisels
Contributor
Forum|alt.badge.img+3
  • Contributor
  • June 1, 2021

Thanks, this is the way, that i will take too.


  • May 9, 2022

Hi,

I had the same problem and this is how I solved it with python:

Create the geojson file with a FeatureWriter. In the summary output port you get the output path as "_dataset" attribute. Connect a PythonCaller to the summary output port and add the following code in the input function:

import json
import os
[...]
def input(self, feature):
    filename = feature.getAttribute('_dataset')
    with open(filename, 'r', encoding='utf8'as file:
        data = json.load(file)
        for f in data["features"]:
            f["id"] = f["properties"]["id"]
 
    os.remove(filename)
    with open(filename, 'w', encoding='utf8'as f:
        json.dump(data, f, ensure_ascii=False)

 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings