openlayer4将geoJson转成feature对象、把wkt格式的数据转化为feature的对象

 使用OpenLayers的format类中的GeoJSON类将GeoJSON数据转换为Feature对象。示例代码如下:

var geoJsonData = { // GeoJSON数据
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [125.6, 10.1]
      },
      "properties": {
        "name": "Point A"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [125.6, 10.1],
          [125.7, 10.2],
          [125.8, 10.3]
        ]
      },
      "properties": {
        "name": "LineString A"
      }
    }
  ]
};
var geoJsonFormat = new ol.format.GeoJSON(); // 创建GeoJSON格式化对象
var features = geoJsonFormat.readFeatures(geoJsonData); // 将GeoJSON数据转换为Feature对象
// 将Feature对象添加到矢量图层中
var vectorLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: features
  })
});
// 添加矢量图层到地图中
var map = new ol.Map({
  target: 'map',
  layers: [vectorLayer],
  view: new ol.View({
    center: [125.6, 10.1],
    zoom: 10
  })
});



v2:
import GeoJSON from 'ol/format/GeoJSON';
const geoJSON = '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,1]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2,2],[3,3],[4,4]]},"properties":{}}]}';
const format = new GeoJSON();
const features = format.readFeatures(geoJSON);
// 确保要素对象被正确识别
console.log(features);

writeFeatures()方法,可以将Feature对象转换为GeoJSON格式的数据

使用ol.format.WKT()对象来将WKT格式的数据转换为ol.Feature对象。

var wkt = 'POLYGON((10 10, 10 20, 20 20, 20 10, 10 10))';
var format = new ol.format.WKT();
var feature = format.readFeature(wkt);
var layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [feature]
  })
});
var map = new ol.Map({
  target: 'map',
  layers: [layer],
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值