我正在寻找一个JavaScript库,可以让我进行地理空间查询。我知道OpenLayers和GoogleMaps,但这两个不支持像联合,交集等操作。
+----+
| |
| +-+--+
+--+-+ |
| |
+----+
至
+----+
| |
| +--+
+--+ |
| |
+----+
我正在寻找一个JavaScript库,可以让我进行地理空间查询。我知道OpenLayers和GoogleMaps,但这两个不支持像联合,交集等操作。
+----+
| |
| +-+--+
+--+-+ |
| |
+----+
至
+----+
| |
| +--+
+--+ |
| |
+----+
更新于2014年4月29日:请查看Turf,看起来非常有前途
JSTS可以在浏览器中对几何对象进行联合。JSTS与openlayers库集成,并扩展了openlayers几何类(例如OpenLayers.Geometry.Polygon),使它们能够进行几何运算。例如:
>> var poly1 = OpenLayers.Geometry.Polygon.createRegularPolygon(
new OpenLayers.Geometry.Point(0.0,0.0), 2, 5);
>> var poly2 = OpenLayers.Geometry.Polygon.createRegularPolygon(
new OpenLayers.Geometry.Point(1.0,1.0), 2, 4);
>> var poly_u = poly1.union(poly2);
>> var poly_d = poly1.difference(poly2);
>> print(poly_u);
POLYGON((1.5667154718422638 -0.4142135623730949,1.1755705045849463 -1.618033988749895,
-1.1755705045849465 -1.6180339887498947,-1.902113032590307 0.618033988749895,
-0.41421356237309503 1.6990562312593451,-0.4142135623730949 2.414213562373095,
2.414213562373095 2.414213562373095,2.414213562373095 -0.4142135623730949,
1.5667154718422638 -0.4142135623730949))
Geoscript JS很不错,如果你想在JS中进行服务器端几何操作。
您可以扩展OpenLayers以支持此操作。我使用OpenLayers的本地函数来实现这一点。尝试一下,也许您需要修复和定制此代码。
// The first object is instanced using data given from gmaps
var objBound1 = new OpenLayers.Bounds();
objBound1.extend(new OpenLayers.LonLat(2,2));
objBound1.extend(new OpenLayers.LonLat(8,8));
// The second object is instanced using data given from gmaps
var objBound2 = new OpenLayers.Bounds();
objBound2.extend(new OpenLayers.LonLat(5,5));
objBound2.extend(new OpenLayers.LonLat(10,10));
// Extract limits from our objects
var arrBound1 = objBound1.toArray();
var arrBound2 = objBound2.toArray();
// Determine an virtual bound. It must contain our two bounds
var intMinLeft = arrBound1.left < arrBound2.left ? arrBound1.left : arrBound2.left;
var intMinTop = arrBound1.top < arrBound2.top ? arrBound1.top : arrBound2.top;
var intMaxRight = arrBound1.right > arrBound2.right ? arrBound1.right : arrBound2.right;
var intMaxBottom = arrBound1.bottom > arrBound2.bottom ? arrBound1.bottom : arrBound2.bottom;
// Search all points of virtual bound, storing the points contained in bound1 or bound2
var objBoundResult = new OpenLayers.Bounds();
for(var intI = intMinLeft; intI < intMaxRight; intI++) {
for(var intJ = intMinTop; intJ < intMaxBottom; intJ++) {
if(objBound1.containsLonLat(new OpenLayers.LonLat(intI, intJ)) || objBound2.containsLonLat(new OpenLayers.LonLat(intI, intJ))) {
objBoundResult.add(intI, intJ);
}
}
}
// objBoundResult is what you want
您可以获取包含多边形的半径或矩形的查询结果,然后使用此处描述的技术过滤结果:http://msdn.microsoft.com/en-us/library/cc451895.aspx。该示例使用 Bing 地图,但您也可以轻松地使用您喜欢的任何地图服务应用相同的原则。
你真的需要在客户端执行这个操作吗?Union 是一个相当重的操作,最好在服务器端完成。
另一个可能有用的 API 是 ArcGIS Javascript API,尽管从我所看到的来看,我认为它没有 ArcGIS Server 就无法执行 Union:http://help.arcgis.com/en/webapi/javascript/arcgis/