package com.tairui.app.cim.util;
|
|
|
import com.vividsolutions.jts.geom.Geometry;
|
import com.vividsolutions.jts.geom.GeometryFactory;
|
import com.vividsolutions.jts.geom.Point;
|
import com.vividsolutions.jts.io.WKBReader;
|
import com.vividsolutions.jts.io.WKTReader;
|
import org.geotools.geojson.geom.GeometryJSON;
|
import org.springframework.util.StringUtils;
|
|
import java.io.IOException;
|
import java.io.StringWriter;
|
|
public class GeoJsonUtil {
|
|
|
public static String getGeoJSON(Point geometry){
|
|
if(geometry==null){
|
return null;
|
}
|
StringWriter writer = new StringWriter();
|
GeometryJSON g = new GeometryJSON();
|
try {
|
g.write(geometry, writer);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
String json = writer.toString();
|
if("null".equals(json)){
|
json = null;
|
}
|
return json;
|
}
|
|
public static Geometry getGeometryFromGeometryBinargy(String hexString){
|
GeometryFactory geometryFactory = new GeometryFactory();
|
WKBReader wktReader = new WKBReader();
|
Geometry geometry = null ;
|
try {
|
geometry = wktReader.read(WKBReader.hexToBytes(hexString));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return geometry ;
|
}
|
|
//geoJSON格式字符串转Point类型
|
public static Geometry getGeometryFromGeoJSON(String geoJSON){
|
|
if(StringUtils.isEmpty(geoJSON)){
|
return null;
|
}
|
|
GeometryJSON g = new GeometryJSON();
|
|
Geometry geometry =null ;
|
|
try {
|
|
geometry=(Geometry)g.read(geoJSON);
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
|
|
return geometry;
|
}
|
|
//数据库AsText查询出来的wkt格式的字符串转为geoJSON返回到前端
|
public static String getGeoJSONFromWKT(String geoWkt){
|
|
if(StringUtils.isEmpty(geoWkt)){
|
return null;
|
}
|
WKTReader reader = new WKTReader();
|
|
String ret = null;
|
try {
|
Geometry geometry = reader.read(geoWkt);
|
StringWriter writer = new StringWriter();
|
GeometryJSON g = new GeometryJSON();
|
g.write(geometry, writer);
|
ret = writer.toString();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return ret;
|
|
}
|
|
}
|