处理WKT面数据自相交问题
作者:互联网
一、引入terraformer
Terraformer 是一个几何工具包,用于处理不同的几何格式和构建地理数据库。
Terraformer 是Esri的一个开源项目,功能如下:
<script src="https://unpkg.com/terraformer@1.0.8"></script>
<script src="https://unpkg.com/terraformer-arcgis-parser@1.0.5"></script>
<script src="https://unpkg.com/terraformer-wkt-parser@1.1.2"></script>
//WKT格式测试数据
var wktData =
"POLYGON((118.48859787838352 37.466855887587926,118.48377465024234 37.44892970037951,118.50637309434643 37.448117310665495,118.50691167095988 37.448054561187064,118.50671711924377 37.4538244183002,118.50667363936907 37.4572565405026,118.51339469876487 37.45753381461357,118.51330829093396 37.45970018196293,118.50023196806373 37.45963985156324,118.50257883055443 37.47144541012767,118.51207915768441 37.47080863641392,118.51199204477498 37.47906744882793,118.50402135503961 37.47895797487516,118.5030309092051 37.47427843413634,118.50294475156741 37.47414946318135,118.48952348689284 37.47297242899381,118.48861941218955 37.46694175571052,118.48861939994451 37.46702756470185,118.48866249318695 37.46696333780628,118.48859787838352 37.466855887587926))";
二、处理流程
- WKT数据转GeoJson
var geojson = Terraformer.WKT.parse(wktData);
- GeoJson数据转ArcGIS数据
var arcgis = Terraformer.ArcGIS.convert(geojson);
- ArcGIS数据使用ArcGIS API 操作
(1)使用simplify()将自相交面分成不自相交的多面;
simplify()对几何图形执行简化操作,该操作会更改给定的几何图形,以使其在拓扑上合法。
var geometry = geometryJsonUtils.fromJson(arcgis);
var geo = geometryEngine.simplify(geometry);
(2)判断每个面的端点个数,点数最多的为结果面,展示到地图上:
var geoRing = null;
for (let i = 0; i < geo.rings.length; i++) {
geoRing = geo.rings[0];
if (geo.rings[i].length > geoRing.length) {
geoRing = geo.rings[i]
}
}
var polygon = new Polygon([geoRing]);
var gh = new Graphic(polygon, symbol);
ghLayer.add(gh)
map.setExtent(polygon.getExtent().expand(1.5)); //定位并放大1.5倍
(3)或者判断每个面的面积,面积最大的为结果面,展示到地图上:
var geoRing = null;
var geoAreas = [];
var area = 0;
for (let i = 0; i < geo.rings.length; i++) {
var poly = new Polygon([geo.rings[i]]);
geoAreas[i] = geometryEngine.geodesicArea(poly, "acres");
geoRing = geo.rings[0];
if (area < geoAreas[i]) {
area = geoAreas[i];
geoRing = geo.rings[i]
}
}
var polygon = new Polygon([geoRing]);
var gh = new Graphic(polygon, symbol);
ghLayer.add(gh)
map.setExtent(polygon.getExtent().expand(1.5)); //定位并放大1.5倍
4.ArcGIS数据转GeoJson
var geojson2 = Terraformer.ArcGIS.toGeoJSON(polygon);
5.GeoJson数据转WKT
var result = Terraformer.WKT.convert(geojson2);
三、处理结果
-
处理前:
-
处理后:
标签:polygon,geoRing,处理,rings,相交,var,geo,WKT,Terraformer 来源: https://blog.csdn.net/ahappyday222/article/details/115401806