首页 / 浏览问题 / 移动GIS / 问题详情
如何将标绘数据转换成JSON?
15EXP 2020年11月17日

使用产品:iMobile v1010 for Android

需求:实现Android端与Web端的标绘数据互相加载

问题:目前Web使用的方法是:

plotting = L.supermap.plotting.getControl(_map, plottingurl);
sitDataManager = plotting.getSitDataManager();
sitDataManager.saveAsSmlFile("situationMap");

实现以JSON格式导出标绘数据:

{
    "smlInfo": {
        "SMLName": null,
        "SMLDesc": null,
        "SMLSeclevel": null,
        "SMLDepat": null,
        "SMLAuthor": null,
        "SMLTime": null
    },
    "mapInfo": {
        "zoom": 10,
        "centerX": 113.80056936978474,
        "centerY": 32.59979750361389
    },
    "layerDatas": [
        {
            "layerName": "plot",
            "spatialAnalystUrl": null,
            "useCanvas2": false,
            "useCanvas": true,
            "isEditable": true,
            "isLocked": false,
            "visibility": true,
            "features": [
                {
                    "type": "GRAPHICOBJECT",
                    "version": 2,
                    "isEdit": true,
                    "uuid": "4c7ceb21-2b20-4d6e-85ef-f1e440aab91b",
                    "associatedUuid": "",
                    "rotate2D": {
                        "x": 0,
                        "y": 0,
                        "z": 0
                    },
                    "scale2D": {
                        "x": 1,
                        "y": 0,
                        "z": 0
                    },
                    "libID": 22,
                    "code": 1002,
                    "algoMaxEditPts": 99999,
                    "algoMinEditPts": 3,
                    "symbolType": 2,
                    "symbolName": "多箭头",
                    "annotationPosition": 4,
                    "textContent": "",
                    "textDisplay": true,
                    "scaleByMap": null,
                    "resolution": null,
                    "isLocked": false,
                    "note": "",
                    "custom": null,
                    "subSymbols": [],
                    "extendProperty": [],
                    "avoidRegions": [],
                    "style": {
                        "fillSymbolID": 1,
                        "fillGradientMode": 0,
                        "fillAngle": 0,
                        "fillGradientAngle": 0,
                        "fillCenterOffsetX": 0,
                        "fillGradientOffsetRatioX": 0,
                        "fillCenterOffsetY": 0,
                        "fillGradientOffsetRatioY": 0,
                        "fillForeColor": {
                            "red": 255,
                            "green": 0,
                            "blue": 0,
                            "alpha": 255
                        },
                        "fillOpaqueRate": 31,
                        "fillBackColor": {
                            "red": 255,
                            "green": 0,
                            "blue": 0,
                            "alpha": 255
                        },
                        "fillBackOpaque": 1,
                        "lineColor": {
                            "red": 255,
                            "green": 0,
                            "blue": 0,
                            "alpha": 255
                        },
                        "lineWidth": 0.5291666666666667,
                        "lineSymbolID": 0,
                        "dashArray": null,
                        "fontStrokeWidth": "2px",
                        "fontBackground": false,
                        "fontBackgroundColor": "#ff0000",
                        "fontShadow": false,
                        "fontShadowColor": "#ff0000",
                        "fontShadowOffsetX": 0,
                        "fontShadowOffsetY": 0,
                        "fontSpace": 0,
                        "fontPercent": 100,
                        "strokeGradientMode": 0,
                        "strokeBackColor": {
                            "red": 255,
                            "green": 0,
                            "blue": 0,
                            "alpha": 255
                        }
                    },
                    "textStyle2D": {
                        "fontName": "Microsoft YaHei",
                        "sizeFixed": true,
                        "fontHeight": 3.1749999999999994,
                        "foreColor": {
                            "red": 0,
                            "green": 0,
                            "blue": 0
                        },
                        "outline": false,
                        "backColor": {
                            "red": 255,
                            "green": 0,
                            "blue": 0
                        },
                        "align": 0
                    },
                    "surroundLineColor": {
                        "red": 255,
                        "green": 255,
                        "blue": 0,
                        "alpha": 255
                    },
                    "surroundLineWidth2D": 1.0583333333333333,
                    "visibility": true,
                    "localePoints": [
                        {
                            "x": 113.81519221440601,
                            "y": 32.808579229595175,
                            "z": 0
                        },
                        {
                            "x": 113.66571424716648,
                            "y": 32.485251887414044,
                            "z": 0
                        },
                        {
                            "x": 114.06378057296735,
                            "y": 32.40238910122692,
                            "z": 0
                        }
                    ],
                    "surroundLineType": 0,
                    "scaleValues": [
                        0
                    ],
                    "subSymbolScaleValue": null,
                    "baseScale": null,
                    "strokeWidth": null,
                    "polylineConnectLocationPoint": false,
                    "arrowHeadType": 0,
                    "arrowBodyType": 0,
                    "arrowTailType": 0
                }
            ]
        }
    ]
}

所以,请问下,Android端应该使用什么方法,也可以实现直接导出这种JSON格式的数据?并且保持数据字段都是一样的?

另外,我用Android的API尝试导出了一份,但不知道为什么自己导出的数据,自己都无法加载?

看以下代码:

// 导出数据至文件
DatasetVector vector = (DatasetVector) dataset;
vector.toGeoJSON(new File(folder, "testGeo"));

// 装载现有的数据文件
DatasetVector vector = (DatasetVector) layer.getDataset();
vector.fromGeoJSON(new File(folder + "testGeo"));

之后提示异常信息:

SMID=20, the geometry is java.lang.Exception: Unsupported GeoJSON Object type: GRAPHICOBJECT
添加或设置geometry失败,请检查SmID和geometry的值,input GeoJSON:{"type":"Feature","geometry":{"type":"GRAPHICOBJECT","libID":100, ......

1个回答

您好,标绘对象是通过CAD数据集来添加的,我们可以获取到CAD数据集里面的对象Geometry,然后输出为JSON格式。使用代码如下:
 

Recordset recordset = ((DatasetVector)dataset).getRecordset(false,CursorType.DYNAMIC);
GeoGraphicObject geometry1 = (GeoGraphicObject) recordset.getGeometry();
String json = geometry1.toGeoJSON();
Log.i("josn",json);
由于我们输出的是标绘对象的json,在前端我们可以使用 plottingLayer.geoJsonToFeature(p,function(){
                alert(1);
            });
9,127EXP 2020年11月17日

你好,感谢回答。

刚才我用以下方法尝试,可以实现导出和加载。但遇到一个新问题,还望解答:

DatasetVector vector = (DatasetVector) dataset;
Recordset recordset = vector.getRecordset(false, CursorType.STATIC);
Geometry geometry = recordset.getGeometry();
geometry.toGeoJSON()

我在地图中添加了3个标绘对象,但通过以上方法,只导出了第一个对象,能不能导出所有的标绘对象呢?

导出多个对象,可以去循环遍历recordset哈。具体可以参考api文档
你好,我没有明白你说的遍历Recordset是怎么遍历的?

我已经查看了Recordset和DatasetVector的API,没有找到对应的遍历方法。
参考代码如下:recordset.moveFirst();
while (!recordset.isEOF()){
   
   
   recordset.moveNext();
}

感谢,原来是这样的,可以了。

最后一个问题。

GeoGraphicObject graphicObject = new GeoGraphicObject();
boolean isSuccessful = graphicObject.fromGeoJSON(geoJson);
if (isSuccessful) recordset.addNew(graphicObject);

Web端导出的数据,我通过以上方法进行导入,但都失败了。

我已经尝试遍历features节点下的数据,逐个添加,也不行。

请问这个有什么办法吗?

web端导出的格式可能不一致,你可以参考移动端这边导出的数据格式,按照这个格式来修改你的数据。或者你直接用移动端导出的json来导入
...