OpenLayers 3获得WKT Polygon字符串

时间:2016-02-05 19:46:56

标签: polygon geospatial openlayers-3 wkt

我有一个openlayers 3地图,我可以在那里绘制Polygon。

我会返回WKT字符串,它表示绘制的多边形。

我该怎么做?

JSFiddle Code http://jsfiddle.net/michelejs/3zawt33b/7/

这是我的地图:

map = new ol.Map({
            target: 'map',
            layers: [raster,vector],
            view: new ol.View({
              center: ol.proj.fromLonLat([11.249367, 43.774298]),
              zoom: 15
            })
        });

这里的插图有助于我绘制多边形:

function addInteraction() {

        var ct = 0;
        draw = new ol.interaction.Draw({
            source: source,
            type: 'Polygon',
            geometryFunction: function (c, g) {
                if (goog.isDef(g)) {
                    g.setCoordinates(c);
                } else {
                    g = new ol.geom.Polygon(c);
                }
                if (c[0].length > ct) {
                    console.log('click coord : ' + c[0][c[0].length - 1]);
                    var coord = c[0][c[0].length - 1];
                    $('div#coordinate').html( $('div#coordinate').html() + "<p>" + ( Number(coord[0]).toFixed(2) ) + " - " + ( Number(coord[1]).toFixed(2) ) + "</p>" );
                coordinates.push(coord);
                    ct = c[0].length;
                } else {
                    console.log('move coord : ' + c[0][c[0].length - 1]);

                }

                return g;
            }
        });
        draw.on('drawend',  function(e) {
            isin = e;
            checkIfIn();
            lastFeature = e.feature;

          //write WKT Polygon Code in div#getAsWK
        })

        draw.on('drawstart', function (e) {
            source.clear();
        });


        map.addInteraction(draw);

    }
   map.addInteraction(draw);

1 个答案:

答案 0 :(得分:2)

ol3包含用于此目的的ol.format.WKT类。 像这样使用writeGeometry()方法:

var format = new ol.format.WKT(),
    wkt = format.writeGeometry(yourFeature.getGeometry());

请参阅API-docs:http://openlayers.org/en/v3.0.0/apidoc/ol.format.WKT.html#writeGeometry

小提琴:http://jsfiddle.net/igor23/3zawt33b/9/