2

I am trying to read the data from a shapefile and once read, I want to convert it to a JSON file.

To read the data I'm using the geoTools library, and to write to JSON, I use the geojson-jackson library (https://github.com/opendatalab-de/geojson-jackson).

I have managed to correctly read the information data extracted from the shapefile, but I cannot get the JSON file to be well generated.

This is part of my code:

Read Data from .shp

public void esShapefileParcel(){

// ShapeFile Directory 
                String archiveshpParcel = directory+"\\"+constant.getTmpParcel()+"\\"+constant.getTmpParcel()+".shp";

                Parcel parcel = new Parcel();

            try {

                File shapeFile = new File (archiveshpParcel);
                FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
                SimpleFeatureSource featureSource = store.getFeatureSource();
                SimpleFeatureCollection collection = featureSource.getFeatures();
                SimpleFeature fParcel;

                SimpleFeatureIterator itrParcel = collection.features();

                while(itrParcel.hasNext()){

                   parcels.clear();

                   fParcel = itrParcel.next();
                   parcel.setExternalReference((String)fParcel.getAttribute("REFCAT"));

                   parcels.add(parcel);

                   ObjectMapper Obj = new ObjectMapper();
                   String jsonStr = Obj.writeValueAsString(parcels.toString());
                   System.out.println(jsonStr); 

                }


        } catch (IOException e) {
            e.printTrace();
        }

            }
        }

And, my Parcel object is:

public class Parcel{

    protected String type;
    private String externalReference;

    Parcel(){};

    Parcel(String type, String externalReference) {
        this.type = "parcel";
        this.externalReference = externalReference;
        }


    public String getType() {
        return type;
    }

    public String getExternalReference() {
        return externalReference;
    }

    public void setExternalReference(String externalReference) {
        this.externalReference = externalReference;
    }


    @Override
    public String toString() {

             return "  \"type\" : \"" + type + "\""+
               ", \"externalReference\" : '" + externalReference + '\'' ;   

    }

But this, only returns a invalid Json:

"[ \"type\" : \"null\", \"externalReference\" : '3545013XG2734N']" "[ \"type\" : \"null\", \"externalReference\" : '8401550XG1670S']" "[ \"type\" : \"null\", \"externalReference\" : '8401522XG1680A']" "[ \"type\" : \"null\", \"externalReference\" : '9293903XG1699C']" "[ \"type\" : \"null\", \"externalReference\" : '9293907XG1699C']"

2

Why not use GeoTools to handle the input and output? Then your code becomes simply something like:

File inFile = new File("/home/ian/Data/states/states.shp");
File outFile = new File("/home/ian/Data/states/states.geojson");
if (outFile.exists()) {
  outFile.delete();
}
outFile.createNewFile();
// Read
DataStore inputDataStore = DataStoreFinder.getDataStore(
        Collections.singletonMap("url", URLs.fileToUrl(inFile)));

String inputTypeName = inputDataStore.getTypeNames()[0];
SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

FeatureSource<SimpleFeatureType, SimpleFeature>
        source = inputDataStore.getFeatureSource(inputTypeName);

FeatureCollection<SimpleFeatureType, SimpleFeature>
        inputFeatureCollection = source.getFeatures();

Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(outFile));
// Write
DataStore newDataStore = DataStoreFinder.getDataStore(params);

newDataStore.createSchema(inputType);
String typeName = newDataStore.getTypeNames()[0];

SimpleFeatureStore featureStore =
        (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

featureStore.addFeatures(inputFeatureCollection);
newDataStore.dispose();
inputDataStore.dispose();

}

If you don't want to export all the attributes of the shapefile features you need to modify newDataStore.createSchema(inputType); to use a new limited schema. This needs something like:

SimpleFeatureType outType = DataUtilities.createSubType(inputType, new String[] {"type","REFCAT"});
newDataStore.createSchema(outType);
| improve this answer | |
  • Thanks for your advice Ian Turton, without a doubt the code is much more efficient in this way. – Mimmetico Jun 11 '19 at 16:53

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.