Skip to main content
1 of 2
unicoletti
  • 7.4k
  • 23
  • 34

You are doing it right and 1 is the status code (SUCCESS). Below is a complete (working) example of queryByShape implemented in Java mapscript (for a vector layer).

Translating the sample into python should be a piece of cake, but I'm not sure how it works with raster layers.

/**
 * query a single layer by specifying a shape (in map coordinates).
 *
 * @param i              the layer to quer
 * @param s the shape whithin which results must be found
 * @return the query results
 */
protected List queryLayerByShape(int i, shapeObj s) {
    List results = new ArrayList();

    layerObj layer = map.getLayer(i);
    if (layer != null && isVisible(layer)) {
        if (log.isDebugEnabled()) {
            log.debug("Querying (by shape) layer: " + layer.getName());
        }
        if (layer.queryByShape(map, s) == mapscript.MS_SUCCESS) {
            if (layer.open() == mapscript.MS_SUCCESS) {
                for (int j = 0; j < layer.getNumResults(); j++) {
                    if (log.isDebugEnabled())
                        log.debug("Layer " + layer.getName() + ", result number:" + j);
                    resultCacheMemberObj resultMember = layer.getResult(j);
                    shapeObj shape = new shapeObj(layer.getType());
                    layer.getShape(shape, resultMember.getTileindex(), resultMember.getShapeindex());
                    if (shape != null) {
                        Map aResult = getResult(shape, layer);

                        results.add(aResult);
                    } else {
                        log.error("Shape " + j + " is null!");
                    }
                }
        // not necessary since 5.6
                //layer.close();
            } else {
                log.error("Cannot open layer: " + layer.getName());
            }
        } else {
            log.info("Query by shape on layer " + layer.getName() + " failed.");
        }
    }
    return results;
}

public shapeObj getSelectionShape(MapRequest request) {
    shapeObj s=new shapeObj(mapscript.MS_SHAPE_POLYGON);
    double x,y;

    String[] coords=request.getSelectionCoordinates().split(";");
    lineObj l=new lineObj();
    for ( int i=0; i<coords.length; i++ ) {
        String[] pair=coords[i].split(",");
        x=XToMapCoordinates(map.getWidth(), map.getExtent(), Double.parseDouble(pair[0]));
        y=YToMapCoordinates(map.getHeight(), map.getExtent(), Double.parseDouble(pair[1]));

        l.add(new pointObj(x, y, 0));
    }
    s.add(l);

    if (log.isTraceEnabled()) {
        log.trace("QueryByShape selectionCoords="+request.getSelectionCoordinates());
    }

    return s;
}
unicoletti
  • 7.4k
  • 23
  • 34