I'm reading a very large json file as an InputStream in Android with JsonReader. This json has a geometry object like so..

 "geometry":{  
      "type":"MultiPolygon",
      "coordinates":[  
         [  
            [  
               [  
                 -7.661981835965626,
                 39.1261981217978
               ],
             [  
                 -7.661983712214173,
                  39.12617044304507
             ],

           ]
        ], 
(...)

What's the best way of parsing this geometry into a WKT string?

I'm using something like this for the other fields, but for the geometry it's a bit complex and confusing.

if (tag.equals("geometry")) {                       
    reader.beginObject();       
    while (reader.hasNext()) {  (...)
  • Are you able to deserialize the JSON into an object and write a conversion for that object into a WKT string? I do it in .NET using a standard JSON deserializing class into a class I set up with the same properties as the JSON object. I convert that JSON object into a shapefile feature after that, but I am sure creating a WKT could be similar. It might be more simple that parsing the string looking for keys and appending their values to a WKT string. – Branco Mar 13 '15 at 11:44
  • The json file is to big. If I try to load it in to a class all at once i run out of memory. It even crashes notepad++ if i try to open it as text. So i have to read like an inputstream, read object by object insert it into the database and start over for the next one. It easy for simple atributes like... if(tag.equals("country")) {country= reader.nextString();} ...but not so easy for geometries. – Miguel Ângelo Belém Mar 13 '15 at 11:55
  • JSON uses a 3-dimensional array of [rings[coordinates[x/y]]]. WKT uses the same basic vertex order, but for multipolygons nests rings in (((outer),(inner)),...,((outer),(inner))), which requires using ring orientation for layout (and may need correction if the ring order is unreliable). Your example does not have a valid ring (4 vertex minimum). You should probably ingest into a list of lists of vertex pairs before attempting to output WKT. – Vince Mar 13 '15 at 12:08
  • 1
    The geometry list is huge. I only pasted a small piece. It's generated by GeoServer. – Miguel Ângelo Belém Mar 13 '15 at 12:11
  • 1
    What is the original source of the data in GeoServer? If it is Postgis, you could do it directly in the DB. – John Powell Mar 14 '15 at 14:23

At the moment I got it working like this. But i have the feeling it's not the best way to do it.

If anybody have a better way to do it, please say it.

I'm using gson 2.3.1

InputStream in = new FileInputStream(context.getExternalFilesDir(null).getPath() + "/" + jsonDatabaseFile); 
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"))

;

                        if (tag.equals("geometry")) {                       
                            reader.beginObject();   
                            while (reader.hasNext()) {  
                                tag = reader.nextName();    
                                // geometry type
                                // ----------------------------------------------------------------------------------
                                if (tag.equals("type")) {                       
                                    type = reader.nextString();
                                }
                                // geometry coordinates
                                // ----------------------------------------------------------------------------------
                                else if (tag.equals("coordinates")) {   
                                    coordinates.append("(");
                                    reader.beginArray();                                    
                                    while (reader.hasNext()) {
                                        coordinates.append("(");
                                        reader.beginArray();                                            
                                        while (reader.hasNext()) {  
                                            coordinates.append("(");
                                            reader.beginArray();                                                
                                            while (reader.hasNext()) {  
                                                reader.beginArray();        
                                                while (reader.hasNext()) {  
                                                    if(!even){                                                  
                                                        coordinates.append((reader.nextString()));
                                                        coordinates.append(" ");
                                                        even = true;
                                                    }
                                                    else{
                                                        coordinates.append((reader.nextString()));                                                  
                                                        coordinates.append(",");
                                                        even = false;
                                                    }
                                                }                                           
                                                reader.endArray();                                              
                                            }
                                            reader.endArray();
                                            coordinates.deleteCharAt(coordinates.length() -1);
                                            coordinates.append("),");               
                                        }
                                        reader.endArray();
                                        coordinates.deleteCharAt(coordinates.length() -1);
                                        coordinates.append("),");                       
                                    }
                                    coordinates.deleteCharAt(coordinates.length() -1);
                                    reader.endArray();
                                }
                                else {
                                    reader.skipValue(); 
                                }                               
                            }                               
                            reader.endObject(); 
                            coordinates.append(")");                                                    
                        }

    (...)
    wkt = type.toUpperCase() + coordinates;

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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