wkt---geometry
wkt中有Z的在转化成geometry的时候会保留z坐标
"POLYGONZ((120.5521 60.6667,121.5521 60.6667,120.6921 61.6667,122.6921 61.6667,120.5521 60.6667))";
public class Test {
public static void main(String args[]){
String str = "POLYGON((120.5521 60.6667,121.5521 60.6667,120.6921 61.6667,122.6921 61.6667,120.5521 60.6667))";
Geometry t = wktToGeometry(str) ;
System.out.println("result:"+t);
}
/*
生成postgis的几何类型
*/
private static Geometry wktToGeometry(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
return geom;
}
}
geometry---wkt
map--geometry
只要有几何类型和坐标即可转化成geometry
JSONObject jo= JSONObject.parseObject(b.getGeometry()) ;
Map<String,Object> pa = new HashMap<>();
pa.put("type",jo.get("type"));
pa.put("coordinates",jo.get("coordinates"));
JSONObject jos= (JSONObject) JSONObject.toJSON(pa);
String wkt = ReadFileUtils.jsonToWkt(jos);
b.setGeometry(wkt);
public static String jsonToWkt(JSONObject jsonObject) {
String wkt = null;
String type = jsonObject.getString("type");
GeometryJSON gJson = new GeometryJSON();
try {
Reader reader = GeoJSONUtil.toReader(jsonObject.toString());
Geometry read = gJson.read(reader);
// wkt=read.toText();
wkt= CoordinateUtils.geometryToWkt(read,3);
} catch (Exception e){
System.out.println("GeoJson转WKT出现异常");
e.printStackTrace();
}
return wkt;
}
public static String geometryToWkt(Geometry geo, int dimension) {
WKTWriter wktWriter = new WKTWriter(dimension);
String wkt = wktWriter.write(geo);
// wkt = wkt.replace(" Z", "Z");
wkt = wkt.replace(" Z", "");
return wkt;
}
wkt中有Z的在转化成geometry的时候会保留z坐标
wkt和pg中geometry的转化
wkt 转pg geometry
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
if (parameter.startsWith(POLYGON)) {
PGgeometry pGgeometry = new PGgeometry();
Polygon polygon = new Polygon(parameter);
polygon.setSrid(4326);
pGgeometry.setGeometry(polygon);
ps.setObject(i, pGgeometry);
} else if (parameter.startsWith(MULTIPOLYGON)) {
PGgeometry pGgeometry = new PGgeometry();
MultiPolygon multiPolygon = new MultiPolygon(parameter);
multiPolygon.setSrid(4326);
pGgeometry.setGeometry(multiPolygon);
ps.setObject(i, pGgeometry);
} else if (parameter.startsWith(LINESTRING)) {
PGgeometry pGgeometry = new PGgeometry();
LineString lineString = new LineString(parameter);
lineString.setSrid(4326);
pGgeometry.setGeometry(lineString);
ps.setObject(i, pGgeometry);
} else {
throw new BizException(ResultCode.INVALID_FORMAT_ERROR);
}
}
pg中 geometry 转 wkt
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String geo = rs.getString(columnName);
PGgeometry pGgeometry = new PGgeometry(geo);
if (pGgeometry.getGeometry() instanceof LineString) {
LineString lineString = (LineString) pGgeometry.getGeometry();
StringBuffer sb = new StringBuffer();
lineString.outerWKT(sb);
return sb.toString();
} else if (pGgeometry.getGeometry() instanceof Polygon) {
Polygon polygon = (Polygon) pGgeometry.getGeometry();
StringBuffer sb = new StringBuffer();
polygon.outerWKT(sb);
return sb.toString();
} else if (pGgeometry.getGeometry() instanceof MultiPolygon) {
MultiPolygon multiPolygon = (MultiPolygon) pGgeometry.getGeometry();
StringBuffer sb = new StringBuffer();
multiPolygon.outerWKT(sb);
return sb.toString();
} else {
throw new BizException(ResultCode.INTERNAL_ERROR);
}
}