从字符串中读取GeometryCollection

时间:2016-09-02 16:10:05

标签: openlayers-3

在OpenLayers 3中,我可以通过ol.format.GeoJSON()的实例从GeoJSON字符串中读取要素或单个几何。 例如:

var parser = new ol.format.GeoJSON();
var arrOfFeatures = parser.readFeatures(geoJsonString);
//or
var usrGeom = parser.readGeometry(geoJsonString);

现在我有一个表示GeometryCollection的字符串(不是MultiPoint,MultiLine或MultyPolygon,也不是FeatureCollection)。

我找不到readGeometryCollection(GeoJSONstring)或其他东西的方法,因为结果有ol.geom.GeometryCollection个对象。

任何人都可以帮忙吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

好的,解决方案是读取GeometryCollection,就像任何其他Openlayers ol.geom.Geometry一样,然后循环读取任何单个几何体,因此:1)将字符串读入Geometry对象,2)if .getType()是" GeometryCollection"然后循环集合,并且集合中的forEach(item)读取Geometry。

parseSingleGeometry : function(geometry, callback) {

        switch (geometry.getType()) {
            case 'MultiLineString':
                geometry.getLineStrings().forEach(function (p) {
                    var newFeat = new ol.Feature({ geometry: p });
                    _features.push(newFeat);
                });
                break;
            case 'MultiPolygon':
                geometry.getPolygons().forEach(function (p) {
                    var newFeat = new ol.Feature({ geometry: p });
                    _features.push(newFeat);
                });
                break;
            case 'Polygon':
            case 'LineString':
            case 'Point':
                var newFeat = new ol.Feature({ geometry: geometry });
                _features.push(newFeat);
                delete newFeat;
                break;
            default:
                break;
        }

        if (callback) { callback(); }

    },

    parseGeometryCollection: function(collection, callback) {

        var ciccio = new ol.geom.GeometryCollection();
        //ciccio.getType
        var itemCount = ciccio.getGeometries().length;
        var itemProg = 0;

        ciccio.getGeometries().forEach(function (item) {
            parseSingleGeometry(item, function () {

                itemProg++;
                if (itemProg == itemCount) {
                    if (callback) {
                        callback();
                    }
                }                
            })
        });
    }

[...]

importGeoJson: function (geoJsonString) {

        var parser = new ol.format.GeoJSON();

        var projection = parser.readProjection(geoJsonString);
        var usrProjCode = projection.getCode();

        var convert = false;

        if (usrProjCode != 'EPSG:3857') {
            var msg = "The file provided is in the " + usrProjCode + " projection. The system supports the EPSG:3857 projection only.\n\rWe are converting your geometries in EPSG:3857.\n\rPlease visually check that the added shapes are what your file defines.";
            alert(msg);
            convert = true;
        }

        //if it's a feature or feature collection than I can readFeatures()
        if (geoJsonString.indexOf("Feature") !== -1) {

            console.log("reading feature(s)");

            var arrOfFeatures = parser.readFeatures(geoJsonString);

            arrOfFeatures.forEach(function (item) {

                var usrGeom = item.getGeometry();

                var newGeom = (convert) ? usrGeom.clone().transform(usrProjCode, 'EPSG:3857') : usrGeom;

                if (newGeom.getType() == "GeometryCollection") {
                    _private.parseGeometryCollection(newGeom, function () { /*do something*/ })
                }
                else {
                    _private.parseSingleGeometry(newGeom, function () { /*do something*/ });
                }
            });
        }
        else { //otherwise work with geometries

            var usrGeom = parser.readGeometry(geoJsonString);

            var newGeom = (convert) ? usrGeom.clone().transform(usrProjCode, 'EPSG:3857') : newGeom;

            if (newGeom.getType() == "GeometryCollection") {
                _private.parseGeometryCollection(newGeom, function () { /*do something*/ })
            }
            else {
                _private.parseSingleGeometry(newGeom, function () { /*do something*/ });
            }
        }

    },

_features是包含地图上显示的功能的ol.Collection。