Chris Holmes今天在GeoServer的blog上贴出了一则新闻,GeoServer的新插件Geojson发布了。所谓GeoJSON本是一个酝酿中的新标准,旨在为WFS请求提供JSON格式的输出。
http://localhost:8181/geoserver/wfs?request=GetFeature&typename=topp:states&maxfeatures=1&outputformat=json
什么是JSON?JSON (JavaScript Object Notation) 是轻量级的数据交换格式,便于人的阅读和理解,也便于程序的解析和生成。JSON与编程语言无关,但是应用的类似的C语言的编程习惯。在JSON中,对象是一个无序的名值对集合,支持字符串、数字、对象、数组和布尔等格式的值。而在JavaScript中,可以通过简单的方法调用将JSON形式的对象转换为JavaScript的对象。
什么是GeoJSON?大家指导传统的WFS请求以GML形式来返回结果,但是解析xml并不是一种高效的方式。简单地说,GeoJSON就是将JSON方式的数据传输引入WFS查询。
传统的WKT描述面状要素
POLYGON((-143.4375 17.578125, -123.75 -44.296875, -47.8125 -23.203125, -54.84375 27.421875, -56.25 27.421875, -143.4375 17.578125))
同样的面状要素用JSON描述
{"type":"Feature", "id":"OpenLayers.Feature.Vector_213", "properties":{}, "geometry":{"type":"Polygon", "coordinates":[[[-143.4375, 17.578125], [-123.75, -44.296875], [-47.8125, -23.203125], [-54.84375, 27.421875], [-56.25, 27.421875], [-143.4375, 17.578125]]]}, "crs":{"type":"EPSG", "properties":{"code":4326}}}
下面即是一个典型的JSON格式的点状feature,它被封装在一个features数组之中:
{ "features": [
{ "id": "alesia",
"title": "Alesia",
"classname": "settlement",
"geometryType": "point",
"spatialCoordinates": [[47.535, 4.478, 0.0]],
"srs": "EPSG:4326",
"center": [47.535, 4.478, 0.0]
}
]
}
典型的JSON格式Map Context,所谓Map Context即OGC的WMC,描述一幅地图配置情况
{ "name": "map-1",
"title": "Pleiades Map 1",
"boundingBox": [-12.0, 32.0, 40.0, 58.0],
"srs": "EPSG:4326",
"layers": [
{ "name": "dphysio",
"title": "Demis Physiography",
"layers": "Bathymetry,Topography,Rivers,Waterbodies",
"styles": ",,,",
"onlineResource": "http:\/\/www2.demis.nl\/mapserver\/request.asp",
"srs": "EPSG:4326"
}
]
}
用OpenLayers可以进行GeoJSON的解析,以下是Tim Schaub's的GeoJSON Viewer的代码
var map, vectors, drawControls, formats, select;
function init(){
map = new OpenLayers.Map('map');
//WMS地图图层
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});
//矢量图层,OpenLayers2.4的新功能,可以进行矢量编辑
vectors = new OpenLayers.Layer.Vector("Vector Layer");
//加入地图
map.addLayers([wms, vectors]);
map.addControl(new OpenLayers.Control.MousePosition());
map.addControl(new OpenLayers.Control.EditingToolbar(vectors));
//选项:hover的响应为真,鼠标移动的feature上响应为颜色的改变
//在选择事件中调用serialize方法,参数又库自动注入
var options = {
hover: true,
onSelect: serialize
};
select = new OpenLayers.Control.SelectFeature(vectors, options);
map.addControl(select);
select.activate();
//format下的两个子类声明
formats = {
wkt: new OpenLayers.Format.WKT(),
geojson: new OpenLayers.Format.GeoJSON()
};
map.setCenter(new OpenLayers.LonLat(0, 0), 1);
}
function serialize(feature) {
//从页面控件获得要生成的格式(WKT,GeoJSON)
var type = document.getElementById("formatType").value;
//调用Format父类的方法write,将feature转为WKT或GeoJSON
var str = formats[type].write(feature);
// 删去HTML字符便于显示(仅用于在页面显示)
str = str.replace(/,/g, ', ');
str = str.replace(/(<)/g, '<');
str = str.replace(/>/g, '> ');
document.getElementById('output').innerHTML = str;
}
function deserialize() {
//从页面元素中获得GeoJSON或WTK
var element = document.getElementById('text');
var type = document.getElementById("formatType").value;
//用Format父类的read方法读取GeoJSON或WTK生成features
var features = formats[type].read(element.value);
//边界,用于视图缩放
var bounds;
if(features) {
//假如只有一个feature也转为Array的形式
if(features.constructor != Array) {
features = [features];
}
for(var i=0; i<features.length; ++i) {
//第一次初始化边界
if (!bounds) {
bounds = features[i].geometry.getBounds();
} else {
//扩展边界
bounds.extend(features[i].geometry.getBounds());
}
}
//将新生成的feature加入vector图层
vectors.addFeatures(features);
map.zoomToExtent(bounds);
var plural = (features.length > 1) ? 's' : '';
element.value = features.length + ' feature' + plural + ' added'
} else {
element.value = 'Bad input ' + type;
}
}
GeoJSON还是一个不成熟的标准,有很多细节还没有确定。GeoServer推出的GeoJSON-plugin-RC1是一个试探性的动作,作者将会根据反响来决定日后是否将这个插件加入到GeoServer中。
分享到:
相关推荐
geoserver中发布geojson服务需要的包。里面有四个jar包,包括:gson-2.3.1.jar,gs-vectortiles-2.15.0.jar,java-vector-tile-1.3.4.jar,protobuf-java-3.6.1.jar
一段简单代码描述了用geoserver wfs服务获取图层数据并返回geojson数据
geoserver-2.17.0-bin.zip
geojson数据转换成arcgisjson数据格式后添加到二三维地图中
纽约市行政区地图Geojson 可用esri geometry api进行解析 空间分析
A complete GeoJSON implementation for Android. Table of Contents Requirements Installation Sample Usage Parsing GeoJSON Creating GeoJSON Requirements Android SDK 8 or Higher Installation Android...
世界所有国家geojson数据,地图加载使用
武汉市geojson数据
geojson-layer-js, 将GeoJSON数据加载到ArcGIS地图中的简单方法 geojson-layer-js将GeoJSON资源加载到地图地图中的简单方法。 这是一个简单的定制层,它使用 Terraformer 将GeoJSON转换为 ArcGIS JSON 。 "应该应...
geojson生成的jar,dll,工具类等
包含全球各国的geojson和国内各地的geojson
geojson-flatten 将文件中的MultiPoint,MultiPolygon,MultiLineString和GeometryCollection几何图形展平为简单的非复杂几何图形。 如果传入FeatureCollection,则返回FeatureCollection。 如果传入单个功能,则...
世界地图Geojson数据,可用于Echarts等图表组件中世界地图图谱绘制
全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图geojson数据,全国地图...
geojson-area, 计算geojson多边形或者多重多边形的面积 geojson区域计算任意 GeoJSON 几何图形内的区域。用法npm install @mapbox/geojson-area示例var geojsonArea = require('@mapb
重庆区县的GeoJson,下载就可以使用与Echarts绘图
本文实例为大家分享了vue+openlayers加载geojson并实现点击弹窗教程,供大家参考,具体内容如下 第一步:安装vue-cli cnpm install -g @vue/cli 第二步:新建一个项目 1.新建项目 (vue-openlayers为项目名),并...
GeoJSON - 一个GeoJSON的实现
echarts地图分布全国主要城市,区县地图json
本范例演示数据集与 GeoJson 传输格式标准的转换,目前支持单点、单线、单面、多点、多线、多面,以及几何对象集合的转入转出,可以广泛应用于 GIS 数据路线规划、管网分析、管网巡查等业务。