develop
develop

Reputation: 141

geojson coordinate conversion to the format of the data coming from PostgreSQL?

this is save to vector code :

 if (vectorType.equalsIgnoreCase("Point")) {
            Point point = new GeometryJSON().readPoint(item.getData());
            lineString.setSRID(3857);
            theEvent.setGeom(point);
            theEvent.setVectorType(vectorType);
        }

after I want to read data from the database format geojson but I need to do the shortcut

I did the following code :

   if(entity.getVectorType().trim().equalsIgnoreCase("Point"))
        {
            JSONObject point = new JSONObject();
            point.put("type", "Point");
            JSONArray coord = new JSONArray("["+entity.getGeom().getX()+","+entity.getGeom().getY()+"]");
            point.put("coordinates", coord);
            geoJson.setData(point.toString());                
        }

but I must do shortcut . how can I do. I think do with geotools. we can use FeatureJson but how ?

Upvotes: 0

Views: 425

Answers (2)

develop
develop

Reputation: 141

I did stackoverflow users solution as the following :

 if(entity.getVectorType().trim().equalsIgnoreCase("Point"))
        {
            GeometryJSON gjson = new GeometryJSON();
            Object obj = JSONValue.parse(gjson.toString(entity.getGeom()));
            System.out.println(obj);
        }

Thank you.

Upvotes: 1

Paweł Głowacz
Paweł Głowacz

Reputation: 3046

It should be like in this example: Point to GeoJSON

Let see your code:

   if(entity.getVectorType().trim().equalsIgnoreCase("Point")){
        geoJson.setData(createJSONGeoPoint(entity.getGeom()));//getGeom() is your Point.
    }

Your new method:

private String createJSONGeoPoint(Point point){
    SimpleFeatureType TYPE = DataUtilities.createType("Test", "Location:Point");
    final GeometryBuilder builder = new GeometryBuilder();
    SimpleFeatureBuilder fBuild = new SimpleFeatureBuilder(TYPE);
    fBuild.add(point);
    SimpleFeature feature = fBuild.buildFeature(null);
    FeatureJSON fjson = new FeatureJSON();
    StringWriter writer = new StringWriter();
    try {
        fjson.writeFeature(feature, writer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return writer.toString();
}

You need to test this with your problem and if it's needed make correction to code.

Upvotes: 1

Related Questions