1

I have a very simple Test Class where I try to parse a simple Shapefile (just a few points) to a GeoJson.

public class ShapefileToGeoJSON {

    public static void test() throws IOException {
        // Specify the path to the shapefile
        File shapefile = new File("C:\\Users\\Franzi\\Desktop\\Pos2Shp\\test\\test.shp");

        Map map = new HashMap();
        map.put( "url", URLs.fileToUrl(shapefile));
        // Load the shapefile into a GeoTools data store
        DataStore dataStore = DataStoreFinder.getDataStore(map);
        String[] typeNames = dataStore.getTypeNames();
        SimpleFeatureCollection featureCollection = dataStore.getFeatureSource(typeNames[0]).getFeatures();

        //Check CRS
        CoordinateReferenceSystem refsystem = featureCollection.getSchema().getCoordinateReferenceSystem();
        
        // Convert the features to GeoJSON format
        FileOutputStream out = new FileOutputStream("C:\\Users\\Franzi\\Desktop\\Pos2Shp\\test9.geojson");
        GeoJSONWriter writer = new GeoJSONWriter(out);    
        writer.setEncodeFeatureCollectionCRS(true);
        writer.setMaxDecimals(10);

        writer.writeFeatureCollection(featureCollection);
        writer.close();

        out.close();

        // Close the data store
        if( dataStore != null ){
            dataStore.dispose();
            dataStore = null;
        }
    }
}

It works fine, put the GeoJSON does not contain any information about the CRS like

"crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },

even though I use writer.setEncodeFeatureCollectionCRS(true);.

The SimpleFeatureCollection has a valid CoordinateReferenceSystem (GCS_WGS_1984). Am I missing something or why is this information not showing up in my GeoJSON?

The GeoJSON looks like this

{
    "type": "FeatureCollection",
    "features": [{
            "type": "Feature",
            "properties": {
                "date": "24/04/2023 09:16:54",
                "Attribute1": "Attribute1"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [9.5124887085, 49.2062567235]
            },
            "id": "test.1"
        }, {
            "type": "Feature",
            "properties": {
                "date": "24/04/2023 09:16:54",
                "Attribute1": "Attribute1"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [9.5126723183, 49.2060909806]
            },
            "id": "test.2"
        }, {
            "type": "Feature",
            "properties": {
                "date": "24/04/2023 09:16:54",
                "Attribute1": "Attribute1"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [9.5126597098, 49.2061072381]
            },
            "id": "test.3"
        }
    ]
}

I am using Android Studio and Geotools 29.0.

Edit: I have also tested my code with a shapefile in UTM, still not showing up. The SimpleFeatureCollection's CoordinateReferenceSystem is correctly set to "ETRS89 / UTM zone 32N".

{
    "type": "FeatureCollection",
    "features": [{
            "type": "Feature",
            "properties": {
                "Date": "28/04/2023 10:20:06",
                "Attribute1": "Attribute1"
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [[[537338.6338488857, 5450491.504021886], [537340.2925363812, 5450492.015228175], [537338.1460640889, 5450491.218172779], [537340.601109487, 5450502.697745162]]]
            },
            "id": "line_utm.1"
        }
    ]
}

1 Answer 1

0

I can't remember if I actually wrote any code for that method but you can check to see. I also suspect that since the default for geojson is wgs84, there is a good chance that we don't bother to write it out to conform to the specification.

1
  • Thanks for your reply! I have tested it with another shpefile with ETRS89 / UTM zone 32N, still no crs field showing up.
    – geoidiot
    Apr 28, 2023 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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