1

I have a (Geo)JSON File with doubles (they represent coordinates). How can I change these numbers? Say I want to add 100.0, 20.0 to all of the coordinates so that [10.0, 50.0] becomes [110.0, 70.0]. I tried using nashorn in java but since I don't know how to use Javascript it didn't work. I would be happy for a java solution but other solutions are ok as well. So what I tried:

    public static void geojsonparser() throws JsonParseException, JsonMappingException, IOException, ScriptException{
    InputStream s1 = new FileInputStream( PATH+"triangle0tolerance2.geojson" );

    FeatureCollection featureCollection = 
            new ObjectMapper().readValue(s1, FeatureCollection.class);

    ScriptEngineManager engineManager = new ScriptEngineManager();
    ScriptEngine engine = engineManager.getEngineByName("nashorn");
    engine.eval("function sum(a, b) { return a + b; }");
    engine.eval("s1.coordinates.forEach(function(coords){ var feat={'type':'Polygon','coordinates':coords};console.log(JSON.stringify(feat));});");
    System.out.println(engine.eval("sum(1, 2);"));
}
and I got the error: Exception in thread "main" javax.script.ScriptException: ReferenceError: "s1" is not defined in <eval> at line number 1

As I mentioned: I would prefer a java solution without javascript :)

Update to Yazans comment: My file looks like this:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 12.0 ], [ 12.0, 12.0 ], [ 12.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 4.0, 4.0 ], [ 5.0, 4.0 ], [ 9.0, 9.0 ], [ 4.0, 9.0 ], [ 4.0, 4.0 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.0, 4.0 ], [ 4.0, 9.0 ], [ 9.0, 9.0 ], [ 9.0, 8.0 ], [ 4.0, 4.0 ] ] ] } }
]
}

And I tried to do as you said. Here my Code:

public static void geojsonparser2() throws JsonParseException,
            JsonMappingException, IOException, ParseException {
        InputStream s1 = new FileInputStream(PATH
                + "triangle0tolerance2.geojson");

        // read the json file
        FileReader reader = new FileReader(PATH + "triangle0tolerance2.geojson");

        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

        // get a String from the JSON object
        String firstName = (String) jsonObject.get("type");
        System.out.println("The type is: " + firstName);

        // get an array from the JSON object
        JSONArray lang = (JSONArray) jsonObject.get("coordinates");

        // take the elements of the json array

        for (int i = 0; i < lang.size(); i++) { //NullPointerException
            System.out.println("The " + i + " coordinate of the array: "
                    + lang.get(i));
        }

    }

But I get a NullpointerException when I try to iterate over the array.

Selphiron
  • 897
  • 1
  • 12
  • 30
  • parse that json into a JsonObject, do wahtever you need to do with those double numbers, then re serialize it (toJson) and send that as your response – Yazan Dec 10 '14 at 10:38
  • Thanks for your comment. I updated the question please take a look. – Selphiron Dec 10 '14 at 11:18
  • 1
    You can't access the coordinates directly. You'll need to navigate throught the json structure. – Paco Abato Dec 10 '14 at 11:22
  • Take a look at http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Paco Abato Dec 10 '14 at 11:44
  • Hmm which specific comment or answer do you mean? I try the exact same thing as the top answer recommends but my array is somehow empty. – Selphiron Dec 10 '14 at 11:53

1 Answers1

1

you must access intermediate objects before getting to "coordinates".

instead of

JSONArray lang = (JSONArray) jsonObject.get("coordinates");

you can do

JSONArray features = (JSONArray) jsonObject.get("features");
JSONObject firstFeature = (JSONObject) features.get(0);
JSONObject geometry = (JSONObject) firstFeature.get("geometry");
JSONArray lang = (JSONArray) geometry.get("coordinates");

be careful to loop thought "features" if needed (here I only get the first occurence)

syllabus
  • 581
  • 3
  • 9
  • ah cool. Now I get [[0.0,0.0],[0.0,12.0],[12.0,12.0],[12.0,0.0],[0.0,0.0]] for lang.get(0)). Now for obtaining each one of them I tried this: while (it.hasNext()) { JSONObject innerObj = (JSONObject) it.next(); } But I get the error: Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject – Selphiron Dec 10 '14 at 14:32
  • indeed, [0.0,0.0] is an array, try JSONArray innerObj = (JSONArray) it.next(); – syllabus Dec 10 '14 at 14:43
  • it worked. Now I can change the entries with `innerObj.set(1, 10.0);` And if I do `System.out.println(lang.get(i));` I can see that it worked. How can I apply the changes to the Jsonfile I read? – Selphiron Dec 10 '14 at 15:06
  • open a FileWriter on the file and write the string returned by "jsonObject.toJSONString()" – syllabus Dec 10 '14 at 15:14
  • For some reason it changes the order of the objects but thats ok. Thank you very much! – Selphiron Dec 10 '14 at 15:33