openlayer feature attributes

 

用这种方法可以得到geoserver里的feature  

 var params = {

REQUEST: "GetFeature",

      //BBOX: new OpenLayers.Bounds(-668087, 3908936, -656113, 3917375).toBBOX(),          //如果不写范围,默认查询所有的feature                                              

       maxFeatures:'50',

       srsName: 'EPSG:2008',

       service: 'WFS',

       version: '1.0.0', 

       //PropertyName: properName,

       typeName:'wltest:sensor_geom'      

     };

        //执行查询

        OpenLayers.loadURL("http://localhost:8080/geoserver/wfs", params, this, setHTML, setHTML); 

 

 

 

 

 

function setHTML(response) {

var gmlParse = new OpenLayers.Format.GML();//如果使用wfs1.1.0,则需要增加如下参数:{xy:false }//更改x,y坐标的读取顺序

/***features 为所有的传感器***/

    var features = gmlParse.read(response.responseText);

    //这就得到了所有的Features

   

    wfs.addFeatures(features);

            map.addLayer(wfs);

}

 

 

你如果想得到feature中的具体属性可以用feature.attributes['id'];//id为属性的名字。但是如果你的表中设置了主键。那个得到主键就不能用这种方式。可以用feature.fid。得到。在处理一下就可以了。

 

 

from osgeo import ogr#用于处理矢量数据 from osgeo import osr#用于处理空间参考和坐标系 import json import requests #发送HTTP请求,获取网络数据 import matplotlib.pyplot as plt#数据可视化 %matplotlib inline class GISFeature: def __init__(self, feature=None):#初始化 self.feature = feature self.geometry = None self.attributes = {} self.coords = [] def read_attributes(self): if self.feature:#确认feature不是None for i in range(self.feature.GetFieldCount()): field_name = self.feature.GetFieldDefnRef(i).GetName() field_value = self.feature.GetField(i) self.attributes[field_name] = field_value print(f"{field_name}: {field_value}") return self.attributes def read_coordinates(self): pass def display(self, ax=None, **kwargs): pass class PointFeature(GISFeature): """点要素类""" def read_coordinates(self): """读取点坐标""" if self.feature:#防止有None self.geometry = self.feature.geometry() x = self.geometry.GetX() y = self.geometry.GetY() self.coords = [(x, y)] return self.coords def display(self, ax=None, color='k', markersize=5, marker='o', **kwargs): """显示点要素""" self.read_coordinates() x, y = zip(*self.coords) if self.coords else ([], []) plt.plot(x, y, marker=marker, markersize=markersize, color=color, **kwargs) # 添加标签(如果有名称属性) if self.attributes and 'name' in self.attributes: plt.text(x[0], y[0], self.attributes['name'], fontsize=8) class LineFeature(GISFeature): """线要素类""" def read_coordinates(self): """读取线坐标""" if self.feature: self.geometry = self.feature.geometry() coords = self.geometry.GetPoints() if coords: self.coords = coords return self.coords def display(self, ax=None, color='r', linewidth=1, **kwargs): """显示线要素""" self.read_coordinates() x, y = zip(*self.coords) plt.plot(x, y, color=color, linewidth=linewidth, **kwargs) if self.attributes and self.attributes.get('name'): plt.text(x[0], y[0], self.attributes['name'], fontsize=8) class PolygonFeature(GISFeature): """面要素类""" def read_coordinates(self): """读取面坐标(外边界)""" if self.feature: self.geometry = self.feature.geometry() ring = self.geometry.GetGeometryRef(0) # 获取外边界 coords = ring.GetPoints() if coords: self.coords = coords return self.coords def display(self, ax=None, facecolor='lightblue', edgecolor='k', **kwargs): """显示面要素""" if not self.coords: self.read_coordinates() x, y = zip(*self.coords) plt.fill(x, y, facecolor=facecolor, edgecolor=edgecolor, **kwargs) plt.plot(x, y, color=edgecolor) class ShapefileLayer: """Shapefile图层管理类""" def __init__(self, file_path): self.file_path = file_path self.dataset = None self.layer = None self.features = [] self.feature_type = None def open_layer(self): """打开图层""" self.dataset = ogr.Open(self.file_path) if self.dataset: self.layer = self.dataset.GetLayer(0) self._detect_feature_type() return True return False def _detect_feature_type(self): """检测要素类型""" if self.layer: geom_type = self.layer.GetGeomType() if geom_type == ogr.wkbPoint: self.feature_type = PointFeature elif geom_type == ogr.wkbLineString: self.feature_type = LineFeature elif geom_type == ogr.wkbPolygon: self.feature_type = PolygonFeature def read_features(self): """读取所有要素""" if not self.layer: self.open_layer() self.features = [] for feature in self.layer: if self.feature_type == PointFeature: gis_feature = PointFeature(feature) elif self.feature_type == LineFeature: gis_feature = LineFeature(feature) elif self.feature_type == PolygonFeature: gis_feature = PolygonFeature(feature) else: continue gis_feature.read_attributes() gis_feature.read_coordinates() self.features.append(gis_feature) return self.features def display_layer(self, **kwargs): """显示整个图层""" if not self.features: self.read_features() for feature in self.features: feature.display( **kwargs) 详细分析一下这段代码,以此为基础生成满足上面条件的新代码,并详细解释
最新发布
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值