I'm working on a Web Map Service in Java. Practically this WMS allows me to represent the map of open data type (shp, kml and gml). I'm going to start working even with data in json format, and for this I am using the GeoJSON libraries GeoTools. My intention is to convert a json file in shp format and parse it. I found several functions of https://gis.stackexchange.com/ but none of it works for me.
I'm using GeoTools 14.0.
Assuming that my WMS arrivals a request like this:
request = GetMap & Layers = hotel, school & Style = Red% 20Point, Black% 20Point
At this point, my WMS picks from this URL the things that I want to represent, namely school and hotel and especially how I want to represent them. Then I do a search for these layers within a directory which I agreed. Inside this folder, I check if a file by that name, and then do a check on its extension. For example:
public void parsing(String layers) {
FileReader f=new FileReader();
f.openFile("C:/Users/...../Servlet/Data");
File[] lista = f.getFiles();
for(File i:lista){
String p=i.getAbsolutePath();
if(i.isDirectory()){
for(File fil:i.listFiles()){
String g=fil.getAbsolutePath();
checkFile(g,layers);
}
}else checkFile(p,layers);
System.out.println("Layers: "+layers +" trovato");
} }
the checkfile function controls me the kind of extension:
public void checkFile(String p,String layers){
if(p.contains(layers)){
if(p.contains("json"))
try {
parsingJSON(p);
found=true;
} catch (IOException e) {
e.printStackTrace();
}
}
My intention is to make the even of open data parsing type "json" and I want to parser this dataset for example, but I should write a function that allows me to parser any json file . school.json
I thought about doing it this way, what do you think?
public FeatureCollection parsingJSON(String p) throws URISyntaxException, SAXException, IOException {
File geojson = new File(p);
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", geojson.toURI().toURL());
try {
DataStore dataStore = DataStoreFinder.getDataStore(params);
if (dataStore == null) {
System.out.println("Error dataStore == null");
}
// read featureCollection
Features handler = new Features();
List<SimpleFeature> features = handler.getFeatures();
collection = new DefaultFeatureCollection();
for(SimpleFeature s:features){
collection.add(s);
}
} catch (IOException e) {
e.printStackTrace();
}
return collection;
}
Unfortunately the line "DataStore dataStore = DataStoreFinder.getDataStore(params);" I generate a terminal error.
Best Answer
To read various file formats for rendering
GeoTools uses a DataStoreFinder mechanism that allows you to call it with a
Map
of keys & parameters and then it searches all the availableDataStoreFactories
to see which one can process those parameters. For your file based stores all you need to do is:GeoTools takes care of the file endings etc and you can render the
FeatureCollection
without the need to write and reparse a shapefile.To Convert one file format to another
GeoTools makes this a relatively simple task - all you need is an input data store (and
DataStoreFinder
will save you looking at file names) and an outputDataStore
of the type you require and some code like:There is a page in the manual which explains how DataStores work in greater detail. You may also benefit from working through the Feature tutorial.
But I suspect that rather than writing your own WMS you would be better off downloading and installing GeoServer which takes care of all of these issues for you.