# Representing json data on a map with GeoTools (Java)

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) {
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");
}
Features handler = new Features();
List<SimpleFeature> features = handler.getFeatures();

collection = new DefaultFeatureCollection();
for(SimpleFeature s:features){
}
} catch (IOException e) {
e.printStackTrace();
}
return collection;
}


Unfortunately the line "DataStore dataStore = DataStoreFinder.getDataStore(params);" I generate a terminal error.

• Welcome to GIS SE. All coding questions in GIS SE are expected to contain code. Please edit the question to contain your coding effort so far. Please take the Tour to better understand how things work here. – Vince Apr 22 '16 at 17:48
• Hi, I have edited my question. I apologize for the incorrect use of the forum. – maxada Apr 23 '16 at 8:46
• If you really want a WMS that uses GeoTools to show shp, kml and gml, why not just deploy GeoServer? – BradHards Apr 23 '16 at 9:07
• Hello @BradHards .I have no constraint, I can use any library. The important thing is that everything is working properly, and follow the pattern that I reported. – maxada Apr 23 '16 at 9:26
• GeoServer is a server, not a library. Suggest looking into it... – BradHards Apr 23 '16 at 11:41

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 available DataStoreFactories to see which one can process those parameters. For your file based stores all you need to do is:

HashMap<String, Object> params = processParams(conv, parameters);
params.put("url",DataUtilities.fileToURL(f));
try {
DataStore ds = DataStoreFinder.getDataStore(params);

if (ds == null) {
//handle error
}


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 output DataStore of the type you require and some code like:

Query query = new Query(inputName.getLocalPart(), filter);
SimpleFeatureCollection inFeatures = input.getFeatureSource(inputName).getFeatures(query);
Transaction transaction = new DefaultTransaction("create");
SimpleFeatureType schema = inFeatures.getSchema();
SimpleFeatureType outSchema = retypeForFormat(schema, output.getClass());
output.createSchema(outSchema);
SimpleFeatureSource featureSource = output.getFeatureSource(outputName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureCollection collection;
if (outSchema != schema) {
List<SimpleFeature> feats = new ArrayList<SimpleFeature>();

FeatureIterator<SimpleFeature> features2 = inFeatures.features();
while (features2.hasNext()) {
SimpleFeature f = features2.next();
SimpleFeature reType = DataUtilities.reType(outSchema, f, true);

reType.setAttribute(outSchema.getGeometryDescriptor().getName(),
f.getAttribute(schema.getGeometryDescriptor().getName()));

}
features2.close();
collection = new ListFeatureCollection(outSchema, feats);
} else {
collection = inFeatures;
}

SimpleFeatureStore outStore = (SimpleFeatureStore) featureSource;
outStore.setTransaction(transaction);

try {
if (verbose) {
System.out.println("copied " + ids.size() + " features");
}
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
output.dispose();
return true;
}


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.

• What do you think of the "parsingJSON" function that I wrote above? – maxada Apr 24 '16 at 13:37
• Please ask a new question if you have further questions – Ian Turton Apr 24 '16 at 13:44