0

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 ?

develop
  • 131
  • 6
  • 16
  • Look at this: http://stackoverflow.com/questions/19311526/how-to-convert-java-object-in-to-geojson-required-by-d3-graph Propably you need to modify creation of `JSONObject` and supply with feature as presented in above example. – Paweł Głowacz Aug 18 '15 at 10:17
  • İs this the same my code ? I must do shorcut :| I think ,I do this in geotool ? @PawełGłowacz – develop Aug 18 '15 at 11:21

2 Answers2

1

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.

Community
  • 1
  • 1
Paweł Głowacz
  • 2,926
  • 3
  • 18
  • 25
1

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.

develop
  • 131
  • 6
  • 16