我是靠谱客的博主 专注哈密瓜,最近开发中收集的这篇文章主要介绍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; } }

最后

以上就是专注哈密瓜为你收集整理的Android中对于Geometry对象的字符串化的全部内容,希望文章能够帮你解决Android中对于Geometry对象的字符串化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部