Spatial Query


Spatial Query finds features that have certain spatial relations with the specified geometric objects. Point, line and region objects have definite boundary rectangles, boundaries, external parts and internal parts. The spatial relations among geometric objects are actually relations among boundary rectangles, boundaries, external parts and internal parts.

Spatial Query is a special query type for spatial data, which can not only realize the query with point objects as centers, but also can implement query with polygons, circles, and other complex geometric objects as centers. For example, if you want to query how many restaurants are there within 5 kilometers of the a conference center, you can use the 5 kilometer Buffer of the conference center as query object together with the Containing condition to query restaurants.

This examples takes World map as an example. According to the objects (point, line, rectangle, circle, polygon) drawn by the user, this example queries all features within the bounds of the geometric object in the specified Cpatials@World.1 and Countries@World layers and adds the results to map as markers.

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");

//Initialize map, and layers
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");


    //After drawing circle, query the Capitals@World.1 layer
    drawPolygon1 = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.RegularPolygon,{handlerOptions:{sides:50}});
    drawPolygon1.events.on({"featureadded": drawCompleted});

    //After drawing circle, query the Capitals@World.1 layer\
    drawPolygon2 = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Polygon);
    drawPolygon2.events.on({"featureadded": drawCompleted});

    //After drawing point, query the Countries@World layer
    drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
    drawPoint.events.on({"featureadded": drawPointCompleted});

    //After drawing line, query the Countries@World layer
    drawLine = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Path);
    drawLine.events.on({"featureadded": drawPointCompleted});

    //After drawing rectangle, query the Capitals@World.1 layer    
    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();
}
//Draw circle
function drawGeometry1() {
    //Clear the previous results
    clearStatus();
    drawPolygon1.activate();
}
//Draw polygon
function drawGeometry2() {
    //Clear the previous results
    clearStatus();
    drawPolygon2.activate();
}
//Draw point
function drawGeometry3() {
    //Clear the previous results
    clearStatus();
    drawPoint.activate();
}
//Draw line
function drawGeometry4() {
    //Clear the previous results
    clearStatus();
    drawLine.activate();
}
//Draw rectangle
function drawGeometry5() {
    //Clear the previous results
    clearStatus();
    drawRectangle.activate();
}

//After drawing, query the Capitals@World.1 layer
function drawCompleted(drawGeometryArgs) {
    var feature = new SuperMap.Feature.Vector();
    feature.geometry = drawGeometryArgs.feature.geometry,
        feature.style = style;
    vectorLayer.addFeatures(feature);
    //Query all features within the bounds of the drawn object in the Capitals@World.1 layer
    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
    });
    //Implement spatial query. Send parameters to server
    queryService = new SuperMap.REST.QueryByGeometryService(url, {
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processFailed
        }
    });
    queryService.processAsync(queryByGeometryParameters);
}

//After drawing, query the Countries@World layer
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);
}
//After drawing, query the Capitals@World.1 layer
function drawRectangleCompleted(obj) {
    drawRectangle.deactivate();
    //Clear the previous results
    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"});//Set parameters with FilterParameter, where name is a required parameter, (layer name format datasetname@datasourcename)
    queryByBoundsParams = new SuperMap.REST.QueryByBoundsParameters({queryParams: [queryParam], bounds: queryBounds});//queryParams array. bounds is the query bounds
    queryService = new SuperMap.REST.QueryByBoundsService(url, {
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processFailed
        }
    });
    queryService.processAsync(queryByBoundsParams);//Send parameters to server and get object back from the server
}

//Return query results and add features in the query results to map as Markers
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();
}

Online Demo and Code Editing

You can access complete source code, experience results, and edit source code to see results in real time.