GeoTools – Reading a GeoJSON via Java API Issue

geojsongeotoolsjava

I'm trying to read this GeoJSON from this string:

{

            "type": "FeatureCollection",

            "features": [

            {

                "type": "Feature",

                "geometry": {

                    "type": "Polygon",

                    "coordinates": [[[-69.0062266588211,

                    9.27562217679211],

                    [-43.517945408821106,

                    -2.811371193331128],

                    [-37.014039158821106,

                    -9.795677582829732],

                    [-72.61157142049046,

                    4.701436497353403],

                    [-69.0062266588211,

                    9.27562217679211]]]

                },

                "properties": {

                    "name": "DOUBLE OAKS CENTER",

                    "address": "1326 WOODWARD AV"

                }

            },

            {

                "type": "Feature",

                "geometry": {

                    "type": "Point",

                    "coordinates": [

                        -80.837753,

                        35.249801

                    ]

                },

                "properties": {

                    "name": "DOUBLE OAKS CENTER",

                    "address": "1326 WOODWARD AV"

                }

            }

            ]

        }

Via this code:

@SuppressWarnings("unchecked")
    public static final FeatureCollection<SimpleFeatureType, SimpleFeature> jsonToFeatureCollection(String jsonContent) throws IOException{

        GeometryJSON geometryJson = new GeometryJSON(15);
        FeatureJSON featureJson = new FeatureJSON(geometryJson);
        Reader stringReader = null;
        @SuppressWarnings("rawtypes")
        FeatureCollection featureCollection = null;

        try{
               stringReader = new StringReader(jsonContent);
               featureCollection = featureJson.readFeatureCollection(stringReader);
        }finally{
                        stringReader.close();
        }
        return featureCollection;

    }

And then re-printing it to check if all is fine, but it seems I don't get the same data that was in the string. In the specific the second geometry that is a Point is reported as a Polygon (with correct coordinates, just repeated geometry=POLYGON ((-80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801))

I print data via this code:

featureCollection = MarshallingTool.jsonToFeatureCollection(this.jsonContent);


                 featureIterator = featureCollection.features();

                 System.out.println("#### FEATURE COLLECTION ####");
                 System.out.println("FeatureCollection: size:" + featureCollection.size());
                 System.out.println("FeatureCollection: ID:" + featureCollection.getID());
                 System.out.println("FeatureCollection: Name:" + featureCollection.getSchema().getName().toString());
                 //System.out.println("FeatureCollection: ID:" + featureCollection.getSchema().getCoordinateReferenceSystem().toWKT());


                 while(featureIterator.hasNext()){
                             aFeature = (SimpleFeature) featureIterator.next();
                             System.out.println("######## FEATURE #######");
                             //System.out.println("Name:" + aFeature.getName());
                             System.out.println("2" + aFeature.getType().getGeometryDescriptor().getName());
                             System.out.println("3" + aFeature.getDefaultGeometryProperty().getType().getName().getLocalPart());
                             System.out.println("4" + aFeature.getIdentifier().getID());
                             System.out.println("5" + aFeature.getType());
                             System.out.println("6" + aFeature.getDefaultGeometryProperty());
                             System.out.println("7" + aFeature.getDefaultGeometry());

                             Map<Object, Object> userData = aFeature.getUserData();
                             Collection<Property> properties = aFeature.getProperties();

                             for(Property property:properties){
                                    System.out.println(property.getName() + "=" + property.getValue());
                             }

                             Set<Object> keys = userData.keySet();

                             for(Object aKey:keys){
                                    System.out.println(aKey + "=" + userData.get(aKey));
                             }

                             System.out.println("##############");         
               }

And when I re-print it I get instead:

#### FEATURE COLLECTION ####
FeatureCollection: size:2
FeatureCollection: ID:featureCollection
FeatureCollection: Name:http://geotools.org:feature
######## FEATURE #######
2geometry
3geometry
4feature-0
5SimpleFeatureTypeImpl http://geotools.org:feature identified extends Feature(name:name,address:address,geometry:geometry)
6GeometryAttributeImpl:geometry=POLYGON ((-69.0062266588211 9.27562217679211, -43.517945408821106 -2.811371193331128, -37.014039158821106 -9.795677582829732, -72.61157142049046 4.701436497353403, -69.0062266588211 9.27562217679211))
7POLYGON ((-69.0062266588211 9.27562217679211, -43.517945408821106 -2.811371193331128, -37.014039158821106 -9.795677582829732, -72.61157142049046 4.701436497353403, -69.0062266588211 9.27562217679211))
name=POLIGON DOUBLE OAKS CENTER
address=POLIGON 1326 WOODWARD AV
geometry=POLYGON ((-69.0062266588211 9.27562217679211, -43.517945408821106 -2.811371193331128, -37.014039158821106 -9.795677582829732, -72.61157142049046 4.701436497353403, -69.0062266588211 9.27562217679211))
##############
######## FEATURE #######
2geometry
3geometry
4feature-1
5SimpleFeatureTypeImpl http://geotools.org:feature identified extends Feature(name:name,address:address,geometry:geometry)
6GeometryAttributeImpl:geometry=POLYGON ((-80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801))
7POLYGON ((-80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801))
name=POINT DOUBLE OAKS CENTER
address=POINT 1326 WOODWARD AV
geometry=POLYGON ((-80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801, -80.837753 35.249801))
##############

Is it possible that the JSON part of the GeoTools API has an issue?
Do you see anything wrong in the way I read the string or in how I print the content ?

Best Answer

Currently, the gt-geojson module is unsupported and doesn't handle features when the first feature is not representative of the whole collection. Therefore your point is interpreted as a polygon because that is what the first feature has.

There is currently a volunteer effort to remove and replace this module with a smarter gt-geojsondatastore but it is currently stalled with a Windows file handling issue.

To quote the unsupported modules page:

If you really need one of these modules to work?

Several of the unsupported modules (for example process) are really popular. That does not however mean that there is anyone in the GeoTools community taking care, fixing bugs and providing documentation. In some cases these modules are the work of students, in others a commercial company that donated the code (but in both cases more resources are needed to make the module usable and trustworthy).

In the past we threw out such work - we are trying a change of policy here by letting you see what can be done (and has been done).

If you really do need one of these modules cleaned up, perhaps for a deadline, please check out our support page - a range of commercial support options are available. Any one of these organisations can be hired to bring these modules up to speed.