1

Can I convert a geometry point to wkt including the srid formatted. See below the example.

"SRID=4326;POINT(-43.9725637435913 -19.9411078674228)".

Although I tried to put point.toText() only return the coordinate without the srid.

1

You can't (currently) read EWKT directly - the simplest route is to split the string at ; and handle the two parts.

  public static void main(String[] args) throws IOException, NoSuchAuthorityCodeException, FactoryException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    WKTReader2 reader = new WKTReader2();
    String line = "";
    while((line=in.readLine())!=null) {
      try {
        Geometry g;
        if(line.contains(";")) {
           String[] parts = line.split(";");
           CRS.decode("EPSG:"+parts[0].substring(5));
            g = reader.read(parts[1]);   
        } else {
          g = reader.read(line);
        }

        System.out.println("\t"+g);
      } catch (ParseException e) {
        System.out.println("invalid input");
        e.printStackTrace();
      }
    }

  }

}

If you want to write EWKT out then you need to look up (and probably cache) the EPSG code from your projection object.

Point p = GenerateRandomData.createRandomPoint();
CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
StringBuffer buffer = new StringBuffer();
buffer.append("SRID=");

buffer.append(CRS.lookupEpsgCode(crs, false)).append(';');
WKTWriter2 writer = new WKTWriter2();
buffer.append(writer.write(p));

System.out.println(buffer.toString());
  • With JTS VividSolutions is the same way , isn't it ? – E. Silveira Jun 7 '17 at 15:55
  • Point p is a JTS Point – Ian Turton Jun 7 '17 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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