3

I have a geojson file with different type of Features like Point line and Polygon as here

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[77.61772155761719,13.073458716478362]}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[77.64141082763672,13.083156825357856],[77.6400375366211,13.080815937441855],[77.63694763183594,13.075130831420575],[77.6345443725586,13.069111164597734],[77.63317108154297,13.064429100005883],[77.63145446777344,13.060081389098263],[77.63076782226562,13.05774028229298],[77.63008117675781,13.055733601658558],[77.63008117675781,13.054730255229568],[77.63008117675781,13.053726904726465],[77.63008117675781,13.052054644835156],[77.63042449951172,13.049713461978671],[77.63042449951172,13.047372256948787],[77.63076782226562,13.047037797277522],[77.63111114501953,13.046703337153813],[77.63179779052734,13.046703337153813],[77.63248443603516,13.046703337153813],[77.6348876953125,13.046703337153813],[77.64106750488281,13.046703337153813],[77.64450073242188,13.046703337153813],[77.64759063720703,13.046703337153813],[77.65274047851562,13.049713461978671],[77.66098022460938,13.057405836652448],[77.66613006591797,13.063091350956306],[77.66956329345703,13.06743900886628],[77.67162322998047,13.06944559438465],[77.67196655273438,13.0697800237184],[77.67196655273438,13.070448881026385],[77.67196655273438,13.070783309000612],[77.67127990722656,13.071117736521622],[77.67059326171875,13.071452163589443],[77.66921997070312,13.072121016365408],[77.66853332519531,13.072121016365408],[77.6678466796875,13.072121016365408],[77.6675033569336,13.072121016365408],[77.66716003417969,13.072121016365408],[77.6675033569336,13.072121016365408]]}}]}

When i read as below

FeatureJSON json = new FeatureJSON();
return json.readFeatureCollection(inputStream);

it read the LineString as Point

4

If your FeatureCollection is not homogenous (i.e. not all the same) the simple GeoJSON parser will not handle your collection, since it bases the schema on the first object.

To read a featureCollection the best approach is to use a GeoJSONDataStore from gt-geojsondatastore module that handles all of that for you.

    URL url = new URL("https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_populated_places.geojson");
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", url);

    store = DataStoreFinder.getDataStore(params);
    String names[] = store.getTypeNames();
    FeatureReader<SimpleFeatureType, SimpleFeature> reader =
            store.getFeatureReader(new Query(names[0], Filter.INCLUDE), null);
  • I get the GeoJson an input stream.. so do I need to save the JSON? – chiranjeevigk Jun 8 '18 at 13:16
  • currently yes, GeoTools has to read the file/stream twice to determine the schema and then to load the features – Ian Turton Jun 8 '18 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.