几何查询


几何查询,是指查询与指定几何对象符合某种空间关系的地物。点、线、面空间几何对象都有明确的边界矩形、边界、内部和外部。几何对象的空间位置关系比较,实质上就是对相互间边界矩形、边界、内部和外部关系的比较。

几何查询是空间数据特有的查询方式,既可以实现以点对象为中心的查询,也可以实现以多边形、圆等复杂几何对象的查询。例如:如果您想知道在奥运场馆周围5公里内有多少家酒店,那么您可以把奥运场馆这个地物(多边形)的5公里Buffer(复杂集合对象)作为查询对象,用Containing条件(包含)去查询酒店图层。

本例将以World地图为例,根据用户绘制的几何对象(点、线、矩形、圆、多边形),查询指定图层中(Capitals@World.1、Countries@World),落在几何对象边界范围内的要素,并把查询得到的点要素作为Marker添加到地图上。

var map, local, layer, vectorLayer, vectorLayer1, drawPolygon, markerLayer,drawPoint, drawLine,
        style = {
            strokeColor: "#304DBE",
            strokeWidth: 1,
            pointerEvents: "visiblePainted",
            fillColor: "#304DBE",
            fillOpacity: 0.5
        },
        url = "http://www.supermapol.com/iserver/services/vm3sbiax/rest/maps/World";
var value = "VZ88xbrMEMpGv4yiisTojgVq",
        name = "ak";
SuperMap.Credential.CREDENTIAL = new SuperMap.Credential(value, "ak");

//初始化地图、用户绘制几何对象的图层、查询结果图层
function init(){
    layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {transparent: true, cacheEnabled: true}, {maxResolution:"auto"});
    layer.events.on({"layerInitialized":addLayer});
    vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
    vectorLayer1 = new SuperMap.Layer.Vector("Vector Layer1");
    markerLayer = new SuperMap.Layer.Markers("Markers");


    //绘制几何圆完成后,查询Capitals@World.1图层
    drawPolygon1 = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.RegularPolygon,{handlerOptions:{sides:50}});
    drawPolygon1.events.on({"featureadded": drawCompleted});

    //绘制几何圆完成后,查询Capitals@World.1图层
    drawPolygon2 = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Polygon);
    drawPolygon2.events.on({"featureadded": drawCompleted});

    //绘制点完成后查询Countries@World图层
    drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
    drawPoint.events.on({"featureadded": drawPointCompleted});

    //绘制线完成后查询Countries@World图层
    drawLine = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Path);
    drawLine.events.on({"featureadded": drawPointCompleted});

    //绘制矩形完成后查询Capitals@World.1图层    
    drawRectangle = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Box);
    drawRectangle.events.on({"featureadded": drawRectangleCompleted});

    map = new SuperMap.Map("map",{controls: [
        new SuperMap.Control.LayerSwitcher(),
        new SuperMap.Control.ScaleLine(),
        new SuperMap.Control.Zoom(),
        new SuperMap.Control.Navigation({
            dragPanOptions: {
                enableKinetic: true
            }}),
        drawPolygon1,drawPolygon2,drawPoint,drawLine,drawRectangle]
                                                             });
}

function addLayer() {
    map.addLayers([layer, vectorLayer, vectorLayer1, markerLayer]);
    map.setCenter(new SuperMap.LonLat(0, 0), 0);
}
function clearStatus(){
    vectorLayer.removeAllFeatures();
    vectorLayer1.removeAllFeatures();
    markerLayer.clearMarkers();
}
//画圆
function drawGeometry1() {
    //先清除上次的显示结果
    clearStatus();
    drawPolygon1.activate();
}
//画多边形
function drawGeometry2() {
    //先清除上次的显示结果
    clearStatus();
    drawPolygon2.activate();
}
//画点
function drawGeometry3() {
    //先清除上次的显示结果
    clearStatus();
    drawPoint.activate();
}
//画线
function drawGeometry4() {
    //先清除上次的显示结果
    clearStatus();
    drawLine.activate();
}
//画矩形
function drawGeometry5() {
    //先清除上次的显示结果
    clearStatus();
    drawRectangle.activate();
}

//绘制完成后开始查询Capitals@World.1图层
function drawCompleted(drawGeometryArgs) {
    var feature = new SuperMap.Feature.Vector();
    feature.geometry = drawGeometryArgs.feature.geometry,
        feature.style = style;
    vectorLayer.addFeatures(feature);
    //根据用户绘制的几何对象,查询Capitals@World.1图层中,落在几何对象边界范围内的要素
    var queryParam, queryByGeometryParameters, queryService;
    queryParam = new SuperMap.REST.FilterParameter({name: "Capitals@World.1"});
    queryByGeometryParameters = new SuperMap.REST.QueryByGeometryParameters({
        queryParams: [queryParam],
        geometry: drawGeometryArgs.feature.geometry,
        spatialQueryMode: SuperMap.REST.SpatialQueryMode.INTERSECT
    });
    //执行几何查询,向服务端传递参数
    queryService = new SuperMap.REST.QueryByGeometryService(url, {
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processFailed
        }
    });
    queryService.processAsync(queryByGeometryParameters);
}

//用户绘制点完成后查询Countries@World图层
function drawPointCompleted(drawGeometryArgs) {
    var feature = new SuperMap.Feature.Vector();
    feature.geometry = drawGeometryArgs.feature.geometry,
        feature.style = style;
    vectorLayer.addFeatures(feature);

    var queryParam, queryByGeometryParameters, queryService;
    queryParam = new SuperMap.REST.FilterParameter({name: "Countries@World"});
    queryByGeometryParameters = new SuperMap.REST.QueryByGeometryParameters({
        queryParams: [queryParam],
        geometry: drawGeometryArgs.feature.geometry,
        spatialQueryMode: SuperMap.REST.SpatialQueryMode.INTERSECT
    });
    queryService = new SuperMap.REST.QueryByGeometryService(url, {
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processFailed
        }
    });
    queryService.processAsync(queryByGeometryParameters);
}
//用户绘制矩形完成后查询Capitals@World.1图层
function drawRectangleCompleted(obj) {
    drawRectangle.deactivate();
    //先清除上次的显示结果
    clearStatus();

    var feature = obj.feature;
    feature.style = style;
    vectorLayer.addFeatures(feature);
    var queryBounds = feature.geometry.bounds;

    var queryParam, queryByBoundsParams, queryService;
    queryParam = new SuperMap.REST.FilterParameter({name: "Capitals@World.1"});//FilterParameter设置查询条件,name是必设的参数,(图层名称格式:数据集名称@数据源别名)
    queryByBoundsParams = new SuperMap.REST.QueryByBoundsParameters({queryParams: [queryParam], bounds: queryBounds});//queryParams查询过滤条件参数数组。bounds查询范围
    queryService = new SuperMap.REST.QueryByBoundsService(url, {
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processFailed
        }
    });
    queryService.processAsync(queryByBoundsParams);//向服务端传递参数,然后服务端返回对象
}

//返回查询结果,并把查询结果中的要素,作为Marker添加到地图上
function processCompleted(queryEventArgs) {
    drawPolygon1.deactivate();
    drawPolygon2.deactivate();
    drawPoint.deactivate();
    drawLine.deactivate();
    drawRectangle.deactivate();
    var i, j, result = queryEventArgs.result;
    if (result && result.recordsets) {
        for (i=0, recordsets=result.recordsets, len=recordsets.length; i<len; i++) {
            if (recordsets[i].features) {
                for (j=0; j<recordsets[i].features.length; j++) {
                    var feature = recordsets[i].features[j];
                    var point = feature.geometry;
                    if(point.CLASS_NAME == SuperMap.Geometry.Point.prototype.CLASS_NAME){
                        var size = new SuperMap.Size(44, 33),
                                offset = new SuperMap.Pixel(-(size.w/2), -size.h),
                                icon = new SuperMap.Icon("http://www.supermapol.com/static/portal/images/markers/mark_blue.png", size, offset);
                        markerLayer.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
                    }else{
                        feature.style = style;
                        vectorLayer1.addFeatures(feature);
                    }

                }
            }
        }
    }
}
function processFailed(e) {
    alert(e.error.errorMsg);
}
function clearFeatures() {
    vectorLayer.removeAllFeatures();
    vectorLayer1.removeAllFeatures();
    markerLayer.clearMarkers();
}

在线演示与源码编辑

您可以在线访问完整代码、体验演示效果,也可以直接在线编辑源码并实时查看效果。