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 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 getSpatialReference() {  
        return spatialReference;  
    }  
    public void setSpatialReference(HashMap spatialReference) {  
        this.spatialReference = spatialReference;  
    }  
}  

MultiIPointObject类(对应MultiIPoint):

public class MultiIPointObject {  
    private List points;  
    private HashMap spatialReference;  

    public List getPoints() {  
        return points;  
    }  
    public void setPoints(List points) {  
        this.points = points;  
    }  
    public HashMap getSpatialReference() {  
        return spatialReference;  
    }  
    public void setSpatialReference(HashMap spatialReference) {  
        this.spatialReference = spatialReference;  
    }  
}  

LineStringObject类(对应 LineString):

public class LineStringObject {  
    private List> paths;  
    private HashMap spatialReference;  
  
    public List> getPaths() {  
        return paths;  
    }  
    public void setPaths(List> paths) {  
        this.paths = paths;  
    }  
    public HashMap getSpatialReference() {  
        return spatialReference;  
    }  
    public void setSpatialReference(HashMap spatialReference) {  
        this.spatialReference = spatialReference;  
    }  
}  

MultLinesStringObject类(对应 MultLinesString):

public class MultLinesStringObject {  
    private List> rings;  
    private HashMap spatialReference;  
  
    public List> getRings() {  
        return rings;  
    }  
    public void setRings(List> rings) {  
        this.rings = rings;  
    }  
    public HashMap getSpatialReference() {  
        return spatialReference;  
    }  
    public void setSpatialReference(HashMap spatialReference) {  
        this.spatialReference = spatialReference;  
    }  
}  

PolygonObject类(对应 Polygon 和 MULTIPOLYGON):

public class PolygonObject {  
    private List> rings;  
    private HashMap spatialReference;  
  
    public List> getRings() {  
        return rings;  
    }  
    public void setRings(List> rings) {  
        this.rings = rings;  
    }  
    public HashMap getSpatialReference() {  
        return spatialReference;  
    }  
    public void setSpatialReference(HashMap 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 spatialReference = new HashMap();  
        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 list = new ArrayList();  
        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[] {  
                        Double.parseDouble(items[0]),  
                        Double.parseDouble(items[1]) };  
                list.add(listResult);  
            } else if (i == strMiddles.length) {  
                String item = strMiddles[i]  
                        .substring(1, strMiddles[i].length());  
                String[] items = item.split(" ");  
                Double[] listResult = new Double[] {  
                        Double.parseDouble(items[0]),  
                        Double.parseDouble(items[1]) };  
                list.add(listResult);  
            } else {  
                String strItem = strMiddles[i].trim();  
                String item = strItem.substring(1, strItem.length() - 1);  
                String[] items = item.split(" ");  
                Double[] listResult = new Double[] {  
                        Double.parseDouble(items[0]),  
                        Double.parseDouble(items[1]) };  
                list.add(listResult);  
            }  
        }  
        HashMap spatialReference = new HashMap();  
        spatialReference.put("wkid", wkid);  
        multiIPointObject.setPoints(list);  
        multiIPointObject.setSpatialReference(spatialReference);  
        Gson gson = new Gson();  
        return gson.toJson(multiIPointObject);  
    }  
  
    /** 
     * 线 转换 JSON 
     * @param wkt 
     * @param wkid 
     * @return 
     */  
    public String getLINESTRINGWktToJson(String wkt, int wkid) {  
        LineStringObject lineStringObject = new LineStringObject();  
        List> lists = new ArrayList>();  
        List list = new ArrayList();  
        String[] strHead = wkt.split("\\(");  
        String strContent = strHead[1].substring(0, strHead[1].length() - 1);  
        String[] strResult = strContent.split(",");  
        for (int i = 0; i < strResult.length; i++) {  
            String itme = strResult[i].trim();  
            String[] items = itme.split(" ");  
            Double[] listResult = new Double[] { Double.parseDouble(items[0]),  
                    Double.parseDouble(items[1]) };  
            list.add(listResult);  
        }  
        lists.add(list);  
        HashMap spatialReference = new HashMap();  
        spatialReference.put("wkid", wkid);  
        lineStringObject.setPaths(lists);  
        lineStringObject.setSpatialReference(spatialReference);  
        Gson gson = new Gson();  
        return gson.toJson(lineStringObject);  
    }  
  
    /** 
     * 多线 转换 JSON 
     * @param wkt 
     * @param wkid 
     * @return 
     */  
    public String getMULTILINESTRINGWktToJson(String wkt, int wkid) {  
        MultLinesStringObject lineStringObject = new MultLinesStringObject();  
        List> lists = new ArrayList>();  
        String ToTailWkt = wkt.substring(0, wkt.length() - 1);  
        String[] strHead = ToTailWkt.split("\\(", 2);  
        String[] strList = strHead[1].split("\\),\\(");  
        for (int i = 0; i < strList.length; i++) {  
            String item = strList[i].trim();  
            item = item.substring(1, item.length() - 1);  
            String[] items = item.split(",");  
            List list = new ArrayList();  
            for (int j = 0; j < items.length; j++) {  
                String jItem = items[j].trim();  
                String[] jItems = jItem.split(" ");  
                Double[] listResult = new Double[] {  
                        Double.parseDouble(jItems[0]),  
                        Double.parseDouble(jItems[1]) };  
                list.add(listResult);  
            }  
            lists.add(list);  
        }  
        HashMap spatialReference = new HashMap();  
        spatialReference.put("wkid", wkid);  
        lineStringObject.setRings(lists);  
        lineStringObject.setSpatialReference(spatialReference);  
        Gson gson = new Gson();  
        return gson.toJson(lineStringObject);  
    }  

   /** 
     * 多边形 转换 JSON 
     * @param wkt 
     * @param wkid 
     * @return 
     */  
    public String getPOLYGONWktToJson(String wkt, int wkid) {  
        PolygonObject polygonObject = new PolygonObject();  
        List> lists = new ArrayList>();  
        String ToTailWkt = wkt.substring(0, wkt.length() - 1);  
        String[] strHead = ToTailWkt.split("\\(", 2);  
        String[] strList = strHead[1].split("\\), \\(");  
        for (int i = 0; i < strList.length; i++) {  
            String item = strList[i].trim();  
            item = item.substring(1, item.length() - 1);  
            String[] items = item.split(",");  
            List list = new ArrayList();  
            for (int j = 0; j < items.length; j++) {  
                String jItem = items[j].trim();  
                String[] jItems = jItem.split(" ");  
                Double[] listResult = new Double[] {  
                        Double.parseDouble(jItems[0]),  
                        Double.parseDouble(jItems[1]) };  
                list.add(listResult);  
            }  
            lists.add(list);  
        }  
        HashMap spatialReference = new HashMap();  
        spatialReference.put("wkid", wkid);  
        polygonObject.setRings(lists);  
        polygonObject.setSpatialReference(spatialReference);  
        Gson gson = new Gson();  
        return gson.toJson(polygonObject);  
    }  
  
   /** 
     * 多个多边形 转换 JSON 
     * @param wkt 
     * @param wkid 
     * @return 
     */  
    public String getMULTIPOLYGONWktToJson(String wkt, int wkid) {  
        PolygonObject polygonObject = new PolygonObject();  
        List> lists = new ArrayList>();  
  
        String ToTailWkt = wkt.substring(0, wkt.length() - 1);  
        String[] strHead = ToTailWkt.split("\\(", 2);  
        ToTailWkt = strHead[1].substring(0, strHead[1].length() - 1);  
        String[] strHeads = ToTailWkt.split("\\(", 2);  
        String[] strList = strHeads[1].split("\\), \\(");  
        if (strList.length == 1) {  
            for (int i = 0; i < strList.length; i++) {  
                String item = strList[i].trim();  
                item = item.substring(1, item.length() - 1);  
                String[] items = item.split(",");  
                List list = new ArrayList();  
                for (int j = 0; j < items.length; j++) {  
                    String jItem = items[j].trim();  
                    String[] jItems = jItem.split(" ");  
                    Double[] listResult = new Double[] {  
                            Double.parseDouble(jItems[0]),  
                            Double.parseDouble(jItems[1]) };  
                    list.add(listResult);  
                }  
                lists.add(list);  
            }  
        } else {  
            for (int i = 0; i < strList.length; i++) {  
                String item = strList[i].trim();  
                item = item.substring(1, item.length() - 1);  
                String[] items = item.split(",");  
                List list = new ArrayList();  
                for (int j = 1; j < items.length; j++) {  
                    String jItem = items[j].trim();  
                    String[] jItems = jItem.split(" ");  
                    Double[] listResult = new Double[] {  
                            Double.parseDouble(jItems[0]),  
                            Double.parseDouble(jItems[1]) };  
                    list.add(listResult);  
                }  
                lists.add(list);  
            }  
        }  
        HashMap spatialReference = new HashMap();  
        spatialReference.put("wkid", wkid);  
        polygonObject.setRings(lists);  
        polygonObject.setSpatialReference(spatialReference);  
        Gson gson = new Gson();  
        return gson.toJson(polygonObject);  
    }  
}  

4、在代码中使用,将获取到的 Json 转换为我们需要的图形。

String geoJson = WKT.getLINESTRINGWktToJson(prjShapeStr, wkid);
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createJsonParser(geoJson);
MapGeometry mapGeometry = GeometryEngine.jsonToGeometry(jsonParser);
Geometry geometry = mapGeometry.getGeometry();  

5、转换完毕,获取到的 geometry 可以直接添加到 arcgis 动态图层中显示了。

参考文章:http://blog.csdn.net/u014014578/article/details/21453727

你可能感兴趣的:(WKT 字符串转为Geometry)