概述:这里说的Arcgis对wms/wfs服务查询结果的解析主要是指Arcgis对geojso的解析。
接上篇:https://www.jianshu.com/p/ea5f5f98c9df
由于arcgis目前并不支持geojson数据,所以这里将geojson数据先转换为wkt字符串,然后在arcgis平台解析使用。
geojson数据样式:
上代码:
使用到的第三方库:
implementation 'com.cocoahero.android:geojson:1.0.1'
implementation 'com.google.code.gson:gson:2.8.0'
在代码中使用:
GeoJSONObject geoJSON = GeoJSON.parse(response);
JSONObject jsonObject = geoJSON.toJSON();
JSONArray features = jsonObject.getJSONArray("features");
if (features.length() > 0) {
for (int i = 0; i < features.length(); i++) {
JSONObject featJob = (JSONObject) features.get(i);
JSONObject geometryJob = featJob.getJSONObject("geometry");
if (geometryJob.getString("type").equals("MultiPolygon")) {
// 将项目信息属性转为键值对并输出
// Properties类为自定义的对象 -- 用于存储图形信息
Properties p = JSON.parseObject(featJob.getString("properties"), Properties.class);
StringBuffer msg = new StringBuffer();
HashMap pMap = (HashMap) obj2Map(p);
for (Map.Entry entry : pMap.entrySet()) {
msg.append(entry.getKey() + " --- " + entry.getValue()).append("\n");
}
// 多面
String geoWktStr = GeoJsonToWkt.multiPolygonToWkt(geometryJob);
/********* ************ *********/
// TODO 在地图上高亮显示
com.esri.core.geometry.Geometry thisGeo = getPrjGeometry(geoWktStr);
Graphic g = new Graphic(thisGeo, new SimpleFillSymbol(Color.BLUE));
Callout callout = mMapView.getCallout();
callout.show(mPoint, message(ArcgisActivity.this, msg.toString()));
// 高亮显示
int aCountId = graphicsLayer.addGraphic(g);
mMapView.setExtent(g.getGeometry(), 300);
/********* ********** ***********/
}
}
对象转Map键值对:https://www.jianshu.com/p/aeb96183f75b
public class GeoJsonToWkt {
// 多面
public static String multiPolygonToWkt(JSONObject geometryJob) {
try {
JSONArray multiGeo = geometryJob.getJSONArray("coordinates");
MultiPolygon multiPolygon = new MultiPolygon();
multiPolygon.setPolygons(multiGeo);
LogUtil.i(multiPolygon.getType());
List polygons = multiPolygon.getPolygons();
StringBuffer geoWktStr = new StringBuffer();
if (polygons.size() > 0) {
geoWktStr.append("MULTIPOLYGON");
geoWktStr.append("(");
for (int q = 0; q < polygons.size(); q++) {
geoWktStr.append("(");
List rings = polygons.get(q).getRings();
for (int m = 0; m < rings.size(); m++) {
geoWktStr.append("(");
List positions = rings.get(m).getPositions();
LogUtil.i(rings.get(m).toString());
for (int n = 0; n < positions.size(); n++) {
if (n == 0) {
geoWktStr.append(positions.get(n).getLongitude() + " " + positions.get(n).getLatitude());
} else {
geoWktStr.append("," + positions.get(n).getLongitude() + " " + positions.get(n).getLatitude());
}
}
if (m != rings.size() - 1) {
geoWktStr.append("),");
} else {
geoWktStr.append(")");
}
}
if (q != polygons.size() - 1) {
geoWktStr.append("),");
} else {
geoWktStr.append(")");
}
}
geoWktStr.append(")");
}
LogUtil.i(geoWktStr.toString());
return geoWktStr.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
// 多线
public static String multiPolylineToWkt(JSONObject geometryJob) {
try {
JSONArray multiGeo = geometryJob.getJSONArray("coordinates");
MultiLineString multiPolygon = new MultiLineString();
multiPolygon.setLineStrings(multiGeo);
LogUtil.i(multiPolygon.getType());
List lineStrings = multiPolygon.getLineStrings();
StringBuffer geoWktStr = new StringBuffer();
if (lineStrings.size() > 1) {
geoWktStr.append("MULTILINESTRING");
geoWktStr.append("(");
} else {
geoWktStr.append("LINESTRING");
}
if (lineStrings.size() > 0) {
for (int q = 0; q < lineStrings.size(); q++) {
geoWktStr.append("(");
List positions = lineStrings.get(q).getPositions();
LogUtil.i(lineStrings.get(q).toString());
for (int p = 0; p < positions.size(); p++) {
if (p == 0) {
geoWktStr.append(positions.get(p).getLongitude() + " " + positions.get(p).getLatitude());
} else {
geoWktStr.append("," + positions.get(p).getLongitude() + " " + positions.get(p).getLatitude());
}
}
if (q != lineStrings.size() - 1) {
geoWktStr.append("),");
} else {
geoWktStr.append(")");
}
}
if (lineStrings.size() > 1) {
geoWktStr.append(")");
}
}
LogUtil.i(geoWktStr.toString());
return geoWktStr.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
// 点
public static String pointToWkt(JSONObject geometryJob) {
try {
JSONArray multiGeo = geometryJob.getJSONArray("coordinates");
Point point = new Point();
point.setPosition(multiGeo);
LogUtil.i(point.getType());
Position position = point.getPosition();
StringBuffer geoWktStr = new StringBuffer();
geoWktStr.append("POINT");
geoWktStr.append("(");
geoWktStr.append(position.getLongitude() + " " + position.getLatitude());
geoWktStr.append(")");
LogUtil.i(geoWktStr.toString());
return geoWktStr.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
由于数据原因,这里只对 多面、多线和点的数据进行了处理,且由于验证数据过少,或许存在bug。故仅做参考!!!
如何在arcgis 中使用wkt数据,请参考:https://www.jianshu.com/p/476896a89d14