WKT 字符串转为Geometry
在 Arcgis for android 中,并没有直接将 wkt 字符转化为 geometry 的接口,所以我们需要先将 wkt 字符先转化为arcgis可用的 json 字符串,再将 json 字符串转化为我们需要的 Geometry图形。
1、Wkt → Json
1、图形种类:图形分为POINT、MULTIPOINT、LINESTRING、MULTILINESTRING、POLYGON、MULTIPOLYGON这几种类型。
POINT ---- 点
MULTIPOINT ---- 多个点
LINESTRING ---- 线
MULTILINESTRING ---- 多条线
POLYGON ---- 多边形
MULTIPOLYGON ---- 多个多边形
对应的ArcGis对于不同类型所需的JSON格式也不相同。
2、分别构建各种类型的实体类(根据实际需求)
PointObject类(对应POINT):
public class PointObject {
private double x;
private double y;
private HashMap<String, Integer> spatialReference;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public HashMap<String, Integer> getSpatialReference() {
return spatialReference;
}
public void setSpatialReference(HashMap<String, Integer> spatialReference) {
this.spatialReference = spatialReference;
}
}
MultiIPointObject类(对应MultiIPoint):
public class MultiIPointObject {
private List<Double[]> points;
private HashMap<String, Integer> spatialReference;
public List<Double[]> getPoints() {
return points;
}
public void setPoints(List<Double[]> points) {
this.points = points;
}
public HashMap<String, Integer> getSpatialReference() {
return spatialReference;
}
public void setSpatialReference(HashMap<String, Integer> spatialReference) {
this.spatialReference = spatialReference;
}
}
LineStringObject类(对应 LineString):
public class LineStringObject {
private List<List<Double[]>> paths;
private HashMap<String, Integer> spatialReference;
public List<List<Double[]>> getPaths() {
return paths;
}
public void setPaths(List<List<Double[]>> paths) {
this.paths = paths;
}
public HashMap<String, Integer> getSpatialReference() {
return spatialReference;
}
public void setSpatialReference(HashMap<String, Integer> spatialReference) {
this.spatialReference = spatialReference;
}
}
MultLinesStringObject类(对应 MultLinesString):
public class MultLinesStringObject {
private List<List<Double[]>> rings;
private HashMap<String, Integer> spatialReference;
public List<List<Double[]>> getRings() {
return rings;
}
public void setRings(List<List<Double[]>> rings) {
this.rings = rings;
}
public HashMap<String, Integer> getSpatialReference() {
return spatialReference;
}
public void setSpatialReference(HashMap<String, Integer> spatialReference) {
this.spatialReference = spatialReference;
}
}
PolygonObject类(对应 Polygon 和 MULTIPOLYGON):
public class PolygonObject {
private List<List<Double[]>> rings;
private HashMap<String, Integer> spatialReference;
public List<List<Double[]>> getRings() {
return rings;
}
public void setRings(List<List<Double[]>> rings) {
this.rings = rings;
}
public HashMap<String, Integer> getSpatialReference() {
return spatialReference;
}
public void setSpatialReference(HashMap<String, Integer> spatialReference) {
this.spatialReference = spatialReference;
}
}
3、开始转换(通过 Google 提供的 Gson.jar 进行 JSON转换)
public class WKT {
/**
* 点 转换 JSON
* @param wkt
* @param wkid
* @return
*/
public String getPOINTWktToJson(String wkt, int wkid) {
String[] strHead = wkt.split("\\(");
String strContent = strHead[1].substring(0, strHead[1].length() - 1);
String[] strResult = strContent.split(" ");
PointObject pointObject = new PointObject();
pointObject.setX(Double.parseDouble(strResult[0]));
pointObject.setY(Double.parseDouble(strResult[1]));
HashMap<String, Integer> spatialReference = new HashMap<String, Integer>();
spatialReference.put("wkid", wkid);
pointObject.setSpatialReference(spatialReference);
Gson gson = new Gson();
return gson.toJson(pointObject);
}
/**
* 多点 转换 JSON
* @param wkt
* @param wkid
* @return
*/
public String getMULTIPOINTWktToJson(String wkt, int wkid) {
MultiIPointObject multiIPointObject = new MultiIPointObject();
String ToTailWkt = wkt.substring(0, wkt.length() - 1);
String[] strHead = ToTailWkt.split("\\(\\(");
String strMiddle = strHead[1].substring(0, strHead[1].length() - 1);
String[] strMiddles = strMiddle.split(",");
List<Double[]> list = new ArrayList<Double[]>();
for (int i = 0; i < strMiddles.length; i++) {
if (i == 0) {
String item = strMiddles[i].substring(0,
strMiddles[i].length() - 1);
String[] items = item.split(" ");
Double[] listResult = new Double