Is it possible to convert between shapefile and geojson using geotools without access to file system? I have that restriction in my web app, but I need to create shapefile as byte array and then do something with that. Unfortunately I only found a way where I need to specify new data store based on file path.


Despite its nice sounding name a "shapefile" is not a single file but a whole bunch (at least 3 probably not more than 10) of related files which share a common base name. This is why GeoTools requires a URL pointing to the base file (x.shp) and then picks up the other files it needs based on that location.

If you are building your shapefile in memory why not just build a FeatureCollection and then dump that out as GeoJSON (or any other supported DataStore)

Note: You will need to be working on master for the GeoJSON store as I haven't backported it (no demand yet).

Update based on comment

With some luck you can access a zipped shapefile using something like this:

URL zip = DataUtilities.fileToURL(new File("../../data/coast.zip"));
URL shape = new URL("jar:"+zip+"!/110m_coastline.shp");
Map<String, URL> params = new HashMap<>();
params.put("url", shape);
DataStore ds = DataStoreFinder.getDataStore(params );
String name = ds.getTypeNames()[0];
SimpleFeatureSource source = ds.getFeatureSource(name);
System.out.println(name+" has "+source.getCount(Query.ALL)+" features");

It's left as an exercise for the reader as to how you determine the contents of the zip file, but I guess you can do it using the Zip classes in java.util.

  • The thing is that I need to implement shapefile export/import functionality. User will upload zip file that needs to be exported as geojson, or user will click export and then I need to create shapefile from the geospatial data from the database and then return the zip file – Jasmin Feb 12 '16 at 10:46
  • see if the updated code helps – Ian Turton Feb 12 '16 at 11:39
  • but it again seems to be accessing the file system? I have a restriction to not use the file system and to not operate with local files. I should only use ram memory – Jasmin Feb 12 '16 at 12:33
  • that is just because that is where my file lives, you must have some reference to the zip file somewhere. Anyway this is wandering off topic. – Ian Turton Feb 12 '16 at 13:32

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.