Android中对于Geometry对象的字符串化

package com.esri.geometry.utils; import com.esri.core.geometry.Envelope; import com.esri.core.geometry.Geometry; import com.esri.core.geometry.MultiPath; import com.esri.core.geometry.Point; import com.esri.core.geometry.Polygon; import com.esri.core.geometry.Polyline; public class GeometryUtils { /** * 将几何对象生成wkt字符串 */ public static String GeometryToWKT(Geometry geometry){ if(geometry == null){ return null; } String geoStr = ""; Geometry.Type type = geometry.getType(); if("Point".equals(type.name())){ Point pt = (Point)geometry; geoStr = type.name()+"("+pt.getX() +" "+pt.getY()+")"; }else if("Polygon".equals(type.name()) || "Polyline".equals(type.name())){ MultiPath pg = (MultiPath)geometry; geoStr = type.name()+"("+""; int pathSize = pg.getPathCount(); for(int j=0;j<pathSize;j++){ String temp = "("; int size = pg.getPathSize(j); for(int i=0;i<size;i++){ Point pt = pg.getPoint(i); temp +=pt.getX() +" "+pt.getY()+","; } temp = temp.substring(0, temp.length()-1)+")"; geoStr +=temp+","; } geoStr = geoStr.substring(0, geoStr.length()-1)+")"; }else if("Envelope".equals(type.name())){ Envelope env = (Envelope)geometry; geoStr = type.name()+"("+ env.getXMin() +","+env.getYMin()+","+env.getXMax()+","+env.getYMax()+")"; }else if("MultiPoint".equals(type.name())){ }else{ geoStr = null; } return geoStr; } /** * 将wkt字符串拼成几何对象 */ public static Geometry WKTToGeometry(String wkt){ Geometry geo = null; if(wkt == null || wkt.equals("")){ return null; } String headStr = wkt.substring(0, wkt.indexOf("(")); String temp = wkt.substring(wkt.indexOf("(")+1, wkt.lastIndexOf(")")); if(headStr.equals("Point")){ String[] values = temp.split(" "); geo = new Point(Double.valueOf(values[0]),Double.valueOf(values[1])); }else if(headStr.equals("Polyline") || headStr.equals("Polygon")){ geo = parseWKT(temp,headStr); }else if(headStr.equals("Envelope")){ String[] extents = temp.split(","); geo = new Envelope(Double.valueOf(extents[0]),Double.valueOf(extents[1]),Double.valueOf(extents[2]),Double.valueOf(extents[3])); }else if(headStr.equals("MultiPoint")){ }else{ return null; } return geo; } private static Geometry parseWKT(String multipath,String type){ String subMultipath = multipath.substring(1, multipath.length()-1); String[] paths; if(subMultipath.indexOf("),(") >=0 ){ paths = subMultipath.split("),(");//多个几何对象的字符串 }else{ paths = new String[]{subMultipath}; } Point startPoint = null; MultiPath path = null ; if(type.equals("Polyline")){ path = new Polyline(); }else{ path = new Polygon(); } for(int i=0;i<paths.length;i++){ String[] points = paths[i].split(","); startPoint = null; for(int j=0;j<points.length;j++){ String[] pointStr = points[j].split(" "); if(startPoint == null){ startPoint = new Point(Double.valueOf(pointStr[0]),Double.valueOf(pointStr[1])); path.startPath(startPoint); }else{ path.lineTo(new Point(Double.valueOf(pointStr[0]),Double.valueOf(pointStr[1]))); } } } return path; } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值