Saturday, 14 September 2019

wms - 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) {
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 params = new HashMap();
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 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.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...