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

    File[] lista = f.getFiles();

    for(File i:lista){
        String p=i.getAbsolutePath();
            for(File fil:i.listFiles()){
                String g=fil.getAbsolutePath();
        }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){
                    try {
                    } catch (IOException e) {

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){
        } catch (IOException e) {
        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
    Commented Apr 22, 2016 at 17:48
  • Hi, I have edited my question. I apologize for the incorrect use of the forum.
    – maxada
    Commented Apr 23, 2016 at 8:46
  • If you really want a WMS that uses GeoTools to show shp, kml and gml, why not just deploy GeoServer?
    – BradHards
    Commented Apr 23, 2016 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
    Commented Apr 23, 2016 at 9:26
  • GeoServer is a server, not a library. Suggest looking into it...
    – BradHards
    Commented Apr 23, 2016 at 11:41

1 Answer 1


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);
try {
  DataStore ds = DataStoreFinder.getDataStore(params);

   if (ds == null) {
      //handle error
   //read featureCollection 

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


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

  SimpleFeatureStore outStore = (SimpleFeatureStore) featureSource;

  try {
    List<FeatureId> ids = outStore.addFeatures(collection);
    if (verbose) {
      System.out.println("copied " + ids.size() + " features");
  } catch (Exception problem) {
  } finally {
  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
    Commented Apr 24, 2016 at 13:37
  • 1
    Please ask a new question if you have further questions
    – Ian Turton
    Commented Apr 24, 2016 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.