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.
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(); 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