EsriJSON与GeoJSON转换工具类
EsriJSON与GeoJSON转换工具类。简单的点线面应该没问题,复杂的可能有问题,没具体测试。大家可以在这个基础上修改。后面用起来慢慢修正吧。package piesat.geo;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils;import java.math.BigDecimal; import java.util.*;public class EsriGeoJsonUtil {public static String esri2geo(String ersiJson){Map geoMap = new HashMap();try {List geoFs = new ArrayList();geoMap.put("type", "FeatureCollection");Map esriMap = (Map) JSON.parse(ersiJson);Object esriFs = esriMap.get("features");if(esriFs instanceof List){esriFs = (List<Map<String, Object>>) esriFs;for(int i=0; i< ((List) esriFs).size(); i++){Map esriF = (Map) ((List) esriFs).get(i);Map geoF = new HashMap();geoF.put("type", "Feature");geoF.put("properties", esriF.get("attributes"));Map<String, Object> geometry = (Map<String, Object>) esriF.get("geometry");if(null != geometry.get("x")){geoF.put("geometry", geoPoint(geometry));}else if(null != geometry.get("points")){geoF.put("geometry", geoPoints(geometry));}else if(null != geometry.get("paths")){geoF.put("geometry", geoLine(geometry));}else if(null != geometry.get("rings")){geoF.put("geometry", geoPoly(geometry));}geoFs.add(geoF);}geoMap.put("features", geoFs);}}catch (Exception e){e.printStackTrace();}return new JSONObject(geoMap).toString();}public static String geo2ersi(String geoJson, String idAttribute){Map esriMap = new HashMap();try {Map geoMap = (Map) JSON.parse(geoJson);esriMap = getEsriGeo(geoMap, idAttribute);Map spatialReference = new HashMap();spatialReference.put("wkid", 4326);esriMap.put("spatialReference",spatialReference);}catch (Exception e){e.printStackTrace();}return new JSONObject(esriMap).toString();}public static Map getEsriGeo(Map geoMap, String idAttribute){Map esriMap = new HashMap();idAttribute = StringUtils.isNotEmpty(idAttribute)? idAttribute:"OBJECTID";String type = geoMap.get("type").toString();switch (type){case "Point":List<BigDecimal> coords = (List<BigDecimal>) geoMap.get("coordinates");esriMap.put("x", coords.get(0));esriMap.put("y", coords.get(1));break;case "MultiPoint":esriMap.put("points",geoMap.get("coordinates"));break;case "LineString":List<Object> coordsList = new ArrayList<>();coordsList.add(geoMap.get("coordinates"));esriMap.put("paths",coordsList);break;case "MultiLineString":esriMap.put("paths",geoMap.get("coordinates"));break;case "Polygon":List<List<List<BigDecimal>>> coordinates = (List<List<List<BigDecimal>>>) geoMap.get("coordinates");List<List<List<BigDecimal>>> rings = orientRings(coordinates);esriMap.put("rings", rings);break;case "MultiPolygon":List<List<List<List<BigDecimal>>>> mcoordinates = (List<List<List<List<BigDecimal>>>>) geoMap.get("coordinates");List<List<List<BigDecimal>>> mrings = flattenMultiPolygonRings(mcoordinates);esriMap.put("rings", mrings);break;case "Feature":if (null != geoMap.get("geometry")) {Map geometry = getEsriGeo((Map) geoMap.get("geometry"), idAttribute);esriMap.put("geometry", geometry);}if(null != geoMap.get("properties")){Map properties = (Map) geoMap.get("properties");if (null != geoMap.get("id")) {properties.put(idAttribute, geoMap.get("id"));}esriMap.put("attributes", properties);}break;case "FeatureCollection":List<Object> esriFs = new ArrayList<>();List<Map> features = (List<Map>) geoMap.get("features");for (int i = 0; i < features.size(); i++) {esriFs.add(getEsriGeo(features.get(i), idAttribute));}esriMap.put("features", esriFs);esriMap.put("geometryType","esriGeometryPolygon");break;case "GeometryCollection":List<Object> esriFsc = new ArrayList<>();List<Map> geometries = (List<Map>) geoMap.get("geometries");for (int i = 0; i < geometries.size(); i++) {esriFsc.add(getEsriGeo(geometries.get(i), idAttribute));}esriMap.put("geometries", esriFsc);esriMap.put("geometryType","esriGeometryPolygon");break;}return esriMap;}public static Map geoPoint(Map<String, Object> geometry){Map geo = new HashMap();geo.put("type", "point");BigDecimal x = (BigDecimal) geometry.get("x");BigDecimal y = (BigDecimal) geometry.get("y");List<BigDecimal> coords = new ArrayList<>();coords.add(x);coords.add(y);geo.put("coordinates", coords);return geo;}public static Map geoPoints(Map<String, Object> geometry){Map geo = new HashMap();List<Object> points = (List<Object>) geometry.get("points");if(points.size()==1){geo.put("type", "Point");geo.put("coordinates", points.get(0));}else{geo.put("type", "MultiPoint");geo.put("coordinates", points);}return geo;}public static Map geoLine(Map<String, Object> geometry){Map geo = new HashMap();List<Object> paths = (List<Object>) geometry.get("paths");if(paths.size()==1){geo.put("type", "LineString");geo.put("coordinates", paths.get(0));}else{geo.put("type", "MultiLineString");geo.put("coordinates", paths);}return geo;}public static Map geoPoly(Map<String, Object> geometry){Map geo = new HashMap();List<List<List<BigDecimal>>> rings = (List<List<List<BigDecimal>>>) geometry.get("rings");if(rings.size()==1){geo.put("type", "Polygon");geo.put("coordinates", rings);}else{List<List<List<List<BigDecimal>>>> coords = new ArrayList();String type = "";int len = coords.size() - 1;for(int i=0; i< rings.size(); i++){if(ringIsClockwise( rings.get(i))){List<List<List<BigDecimal>>> item = new ArrayList<>();item.add(rings.get(i));coords.add(item);len++;}else{coords.get(len).add(rings.get(i));}}if(coords.size() == 1){type="Polygon";}else{type="MultiPolygon";}geo.put("type", type);geo.put("coordinates",coords.size()==1?coords.get(0): coords);}return geo;}public static boolean ringIsClockwise(List<List<BigDecimal>> rings){int total = 0;List<BigDecimal> pt1 = null;List<BigDecimal> pt2 = null;for(int i=0; i< rings.size()-1; i++){pt1 = rings.get(i);pt2 = rings.get(i+1);total += (pt2.get(0).doubleValue() - pt1.get(0).doubleValue())* (pt2.get(1).doubleValue() + pt1.get(1).doubleValue());}return total>=0;}public static List<List<List<BigDecimal>>> orientRings ( List<List<List<BigDecimal>>> polygon) {List<List<List<BigDecimal>>> ringsList = new ArrayList<>();List<List<BigDecimal>> outerRing = closeRing(polygon.get(0));if (outerRing.size() >= 4) {if (!ringIsClockwise(outerRing)) {Collections.reverse(outerRing);}ringsList.add(outerRing);polygon.remove(0);for (int i = 0; i < polygon.size(); i++) {List<List<BigDecimal>> hole = closeRing(polygon.get(i));if (hole.size() >= 4) {if (ringIsClockwise(hole)) {Collections.reverse(hole);}ringsList.add(hole);}}}return ringsList;}public static List<List<BigDecimal>> closeRing (List<List<BigDecimal>> coords) {if (!pointsEqual(coords.get(0), coords.get(coords.size()-1))) {coords.add(coords.get(0));}return coords;}public static boolean pointsEqual (List<BigDecimal> a, List<BigDecimal> b) {for (int i = 0; i < a.size(); i++) {if (a.get(i).compareTo(b.get(i)) != 0) {return false;}}return true;}public static List<List<List<BigDecimal>>> flattenMultiPolygonRings (List<List<List<List<BigDecimal>>>> rings) {List<List<List<BigDecimal>>> polygonList = new ArrayList<>();for (int i = 0; i < rings.size(); i++) {List<List<List<BigDecimal>>> polygons = orientRings(rings.get(i));for (int x = polygons.size() - 1; x >= 0; x--) {List<List<BigDecimal>> polygon = polygons.get(x);polygonList.add(polygon);}}return polygonList;} }
EsriJSON与GeoJSON转换工具类相关推荐
- android 字体像素转换工具类_Android中px与dip,sp与dip等的转换工具类
Android中px与dip,sp与dip等的转换工具类 功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方 ...
- java gson 工具类_GSON 实体 转换工具类
/** * Gson转换工具类 */ public class GsonUtils { /** * @param jsonString * json字符串 * @param cl ...
- android 日期时间类,Android 日期时间等转换工具类
package com.easyder.util; import java.text.SimpleDateFormat; import java.util.Date; /** * 日期时间等转换工具类 ...
- android 字体像素转换工具类_android px,dp,sp大小转换工具
package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类 * * float s ...
- java 视频转码工具类_JavaCV入门指南:FrameConverter转换工具类及CanvasFrame图像预览工具类(javaCV教程完结篇)...
JavaCV入门指南:FrameConverter转换工具类及CanvasFrame图像预览工具类(javaCV教程完结篇) 前言 再此章之前,我们已经详细介绍和剖析了javacv的结构和ffmpeg ...
- java字体颜色编程_Java 字体颜色转换工具类 ColorUtil
Java 字体颜色转换工具类 ColorUtil,Java 字体颜色转换工具类 ColorUtil-- import java.awt.Color; import jxl.format.Colour; ...
- Java 字体颜色转换工具类 ColorUtil
import java.awt.Color; import jxl.format.Colour; /** *字体颜色转换工具类 * @author tanghui * */ pu ...
- 自定义java对象转换工具类
背景 项目中经常有VO.PO.DTO等之间转换,由于apache工具类中BeanUtils.copyProperties及Json序列化反序列化方式转换性能比较低(阿里巴巴规范检查有提示不建议采用). ...
- ImageTools-图片格式转换工具类
/*** 图片转换工具类*/ public final class ImageTools {/*** Transfer drawable to bitmap 把drawable转换为bitmap** ...
最新文章
- qchart折现图_Qt Charts 动态实时折线图绘制
- 创建CSS和处理的一些方法
- DL之CNN:基于CRNN_OCR算法(keras,CNN+RNN)利用数据集(torch,mdb格式)训练来实现新图片上不定长度字符串进行识别—预测过程
- 3.3.1 差错控制(检错编码)
- java为什么需要枚举_java – 什么是枚举,为什么它们有用?
- mysql使用技巧,使用Mysql的小技巧总结
- 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第2节 Stream流式思想概述_1_使用传统的方式,遍历集合,对集合进行过滤...
- 2.3 sklearn中的metrics.roc_curve评价指标
- 论文笔记:Reciprocal Multi-Layer Subspace Learning for Multi-View Clustering
- 浅析软文标题写作的三个问题
- 【论文整理1】On the Continuity of Rotation Representations in Neural Networks
- 25款顶级的jQuery表格插件
- linux 查看.img文件,linux img文件 分区挂载
- 前端程序猿常用网站总结
- 如何才能做好一个软件系统?
- 弗洛伊德算法注意项 floyd
- OpenVAS的安装、使用及实战(GVM,Metasploit使用)
- 绿萝算法下网站站长如何建设外链
- 解决局域网内win7出现IPV4地址冲突的问题
- 03.监督过程组-项目绩效报告
热门文章
- 太空人Wifi天气电子时钟(ESP8266+oled 8针0.96寸)
- Python 实现DNS查询放大攻击
- latex编译时报错:runaway argument?
- win10 1903 回收站属性无法操作问题
- 无法启动此程序,因为dll丢失的解决方法分享
- [BZOJ4338][BJOI2015]糖果(扩展Lucas)
- 提高采购流程效率的方法
- Ubuntu环境下下载pulp遇到的问题解决
- 各大厂商的免费 800电话号码 (经典没事拨来玩)
- windows10 win10官方iso镜像下载