Openlayers中Feature与WKT之间的转换,Feature坐标系的转换 WKT与GEOJSON转换

如wkt为:LINESTRING (106.285126 29.532158, 106.285172 29.531994, 106.285174 29.531986, 106.285295 29.53161)
geojson:"geometry": {
         "type": "Polygon",
         "coordinates": [
           [ [134.04, 38.0], [101.0, 0.0], [101.0, 1.0],
             [134.12, 1.0], [100.0, 0.0] ]
           ]
       }
1、Feature 转WKT 且带坐标系的转换

     var strwkt = new ol.format.WKT().writeFeature(feature, {
                dataProjection: targetcrs,//目标坐标系
                featureProjection:crs  //当前坐标系
            });

 2、WKT转Feature 

  var newfeature = new ol.format.WKT().readFeature(strwkt);

3、Feature 坐标系转换(支持所有图形)

  var geom = feature.getGeometry().transform(crs, targetcrs);
  feature.setGeometry(geom);

4、利用WKT转换Feature坐标系(这种不支持GEOMETRY不为Circle)

  var strwkt = new ol.format.WKT().writeFeature(feature, {
                dataProjection: targetcrs,
                featureProjection:crs
            });
            var newfeature = new ol.format.WKT().readFeature(strwkt);

注意:方法4得到的Feature 会没有原始Feature对应的Properties

5、wkt转geoJSON:
var wkt_c=new ol.format.WKT();
var geometry=wkt_c.readGeometry(wkt);
var convertor = new ol.format.GeoJSON();
var geojson=convertor.writeGeometry(geometry);


//openlayer4.0数据可以不用转换使用如:
var source = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
      });

猜你喜欢

转载自blog.csdn.net/Light_10/article/details/140485311
今日推荐