Question

Error when recreating GeoJSON from attribute

  • 27 January 2017
  • 5 replies
  • 16 views

Hi

I'm new to JSON and GeoJSON, but I have some text for a geometry generated by Mapbox coded as GeoJSON:

"{"type":"FeatureCollection","features":[{"id":"c2fc8b172269fb5b220d5661e804a7e5","type":"Feature","properties":{},"geometry":{"coordinates":[[[5.7289984618329015,58.97117391031097],[5.727435803187348,58.97244926566199],[5.725808033947516,58.97459712581869],[5.728542686566385,58.97520118738589],[5.729714680561443,58.974630685071816],[5.728868240463584,58.973858813964995],[5.728314798897173,58.9731876075123],[5.729193794339011,58.972449265591706],[5.730040234436871,58.971274597908206],[5.7289984618329015,58.97117391031097]]],"type":"Polygon"}}]}"

In my workspace I have added a creator with an attribute containing this text. I the add a Geometry-replacer and choose GeoJSON. Upon running the workspace I get syntax errors.

JSONFragmenter(JSONQueryFactory): A JSON syntax error was found at line 1, column 551

JSONFragmenter(JSONQueryFactory): Unexpected non-whitespace characters were found after the JSON text

The value of the import attribute 'geom2' contained JSON text, but could not be recognized as '%1'

However, the attribute is validated as correct JSON using the attributevalidator but not using the JSONValidator. "Unexpected non-whitespace characters were found after the JSON text" is reported. Meaning the " at the end of the text. Removing this does not remedy the situation.

 

Whats weird is that if I store the text in a text-file and change the extension to JSON, the GeoJSON-reader in FME produces the described polygon, but also reporting about an unexpected character. If I remove it in the JSON-file the workspace generates the polygon without errors.

 

My problem is that I want to store these text segments as an attribute, and later generate the geometry, not from a file using the reader, but ideally using the GeometryReplacer or similar.

 

Anyone have an idea on what can be done?


5 replies

Userlevel 4

The problem is that the GeoJson above isn't a single feature, but a FeatureCollection, i.e. like an array of features containing both attributes and geometries. When you use the GeoJson reader, that is what it expects, so that's why it works.

When you use the GeometryReplacer, however, it expects a single feature and then only the geometry part:

{"coordinates": [[
  [5.7289984618329015, 58.97117391031097],
  [5.727435803187348, 58.97244926566199],
  [5.725808033947516, 58.97459712581869],
  [5.728542686566385, 58.97520118738589],
  [5.729714680561443, 58.974630685071816],
  [5.728868240463584, 58.973858813964995],
  [5.728314798897173, 58.9731876075123],
  [5.729193794339011, 58.972449265591706],
  [5.730040234436871, 58.971274597908206],
  [5.7289984618329015, 58.97117391031097]]],
  "type": "Polygon"
}

Result:

0684Q00000ArJviQAF.png

You can use e.g. the JSONExtractor or JSONFlattener to pre-process your GeoJson block before the GeometreyReplacer.

Userlevel 2
Badge +17

Hi @pfetob, @david_r is right. You will have to decompose the FeatureCollection object into individual Feature objects and extract "geometry" object from each feature. There could be various approaches, but I would use the JSONFragmenter with this parameters setting.

  • JSON Query: json["features"][*]
  • Flatten Query Result into Attributes: Yes
  • Recursively Flatten Objects/Arrays: No
  • Attributes to Expose: id, geometry

You can then create a polygon geometry from the exposed attribute called "geometry" using the GeometryReplacer.

The problem is that the GeoJson above isn't a single feature, but a FeatureCollection, i.e. like an array of features containing both attributes and geometries. When you use the GeoJson reader, that is what it expects, so that's why it works.

When you use the GeometryReplacer, however, it expects a single feature and then only the geometry part:

{"coordinates": [[
  [5.7289984618329015, 58.97117391031097],
  [5.727435803187348, 58.97244926566199],
  [5.725808033947516, 58.97459712581869],
  [5.728542686566385, 58.97520118738589],
  [5.729714680561443, 58.974630685071816],
  [5.728868240463584, 58.973858813964995],
  [5.728314798897173, 58.9731876075123],
  [5.729193794339011, 58.972449265591706],
  [5.730040234436871, 58.971274597908206],
  [5.7289984618329015, 58.97117391031097]]],
  "type": "Polygon"
}

Result:

0684Q00000ArJviQAF.png

You can use e.g. the JSONExtractor or JSONFlattener to pre-process your GeoJson block before the GeometreyReplacer.

Ah, of course. In my thinking I reckoned that setting the geometryreplacer to json would make it behave like the reader. Thanks!

 

 

Hi @pfetob, @david_r is right. You will have to decompose the FeatureCollection object into individual Feature objects and extract "geometry" object from each feature. There could be various approaches, but I would use the JSONFragmenter with this parameters setting.

  • JSON Query: json["features"][*]
  • Flatten Query Result into Attributes: Yes
  • Recursively Flatten Objects/Arrays: No
  • Attributes to Expose: id, geometry

You can then create a polygon geometry from the exposed attribute called "geometry" using the GeometryReplacer.

Thanks takashi. I was able to generate the polygon using the jsonextractor as david_r suggested, but your suggestion worked right "out of the box"- so to speak. Thanks!

 

 

Badge

do you have sample workbench i can look at.

Reply