简体   繁体   中英

Getting Layer Feature Information from GeoServer (Google Maps)

I am writing an Android App with Android Studio (using Java). The app is using Google Maps and has a layer with field ownership information that it's getting from a geoserver. The code to set this up is as follows, and is working well.

public class App1Step1 extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app1_step1);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        TileProvider tileProvider = TileProviderFactory.getTileProvider();
        mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
    }
}

class TileProviderFactory {
    private static final String GEOSERVER_FORMAT =
            "http://xxxxxxxx.at/geoserver/wms" +
                    "?service=WMS" +
                    "&version=1.1.1" +
                    "&request=GetMap" +
                    "&layers=satgrass:INSPIRE_SCHLAEGE_2020_POLYGON" +
                    "&bbox=%f,%f,%f,%f" +
                    "&width=256" +
                    "&height=256" +
                    "&srs=EPSG:900913" +
                    "&format=image/png" +
                    "&transparent=true";

    static TileProvider getTileProvider() {
        TileProvider tileProvider = new WMSTileProvider(256,256) {

            @Override
            public synchronized URL getTileUrl(int x, int y, int zoom) {
                double[] bbox = getBoundingBox(x, y, zoom);
                String s = String.format(Locale.US, GEOSERVER_FORMAT, bbox[MINX], bbox[MINY], bbox[MAXX], bbox[MAXY]);
                
                URL url = null;
                
                try {
                    url = new URL(s);
                } catch (MalformedURLException e) {
                    throw new AssertionError(e);
                }
                
                return url;
            }
        };

        return tileProvider;
    }
}

abstract class WMSTileProvider extends UrlTileProvider {
    // Web Mercator n/w corner of the map.
    private static final double[] TILE_ORIGIN = {-20037508.34789244, 20037508.34789244};
    //array indexes for that data
    private static final int ORIG_X = 0;
    private static final int ORIG_Y = 1; // "

    // Size of square world map in meters, using WebMerc projection.
    private static final double MAP_SIZE = 20037508.34789244 * 2;

    // array indexes for array to hold bounding boxes.
    protected static final int MINX = 0;
    protected static final int MAXX = 1;
    protected static final int MINY = 2;
    protected static final int MAXY = 3;
    
    // Construct with tile size in pixels, normally 256, see parent class.
    public WMSTileProvider(int x, int y) {
        super(x, y);
    }

    // Return a web Mercator bounding box given tile x/y indexes and a zoom
    // level.
    protected double[] getBoundingBox(int x, int y, int zoom) {
        double tileSize = MAP_SIZE / Math.pow(2, zoom);
        double minx = TILE_ORIGIN[ORIG_X] + x * tileSize;
        double maxx = TILE_ORIGIN[ORIG_X] + (x+1) * tileSize;
        double miny = TILE_ORIGIN[ORIG_Y] - (y+1) * tileSize;
        double maxy = TILE_ORIGIN[ORIG_Y] - y * tileSize;

        double[] bbox = new double[4];
        bbox[MINX] = minx;
        bbox[MINY] = miny;
        bbox[MAXX] = maxx;
        bbox[MAXY] = maxy;

        return bbox;
    }
}

The code above will produce the following overlay on Google Maps. On the geoserver, the layer is saved as EPSG:31287 (Austria Lambert).

在此处输入图像描述

What I would like to do now is to get the feature information, when I click on one of the fields. I tried around a lot, but am unable to figure out how to calculate the necessary values (width, height, x, y, bbox) to get the feature information from the latitude and longitude I clicked at. To be specific, these are the values from the query that I'm missing.

String url = "http://xxxxxxxxxxx.at/geoserver/wms" +
                "?service=WMS" +
                "&version=1.1.1" +
                "&request=GetFeatureInfo" +
                "&layers=satgrass:INSPIRE_SCHLAEGE_2020_POLYGON" +
                "&query_layers=satgrass:INSPIRE_SCHLAEGE_2020_POLYGON" +
                "&exceptions=application/vnd.ogc.se_inimage" +
                "&x=" +                             // ????????
                "&y=" +                             // ????????
                "&bbox=" +                          // ????????
                "&width=" +                         // ????????
                "&height=" +                        // ????????
                "&srs=EPSG:900913" +                // EPSG:900913 or EPSG:31287 ?
                "&format=image/png" +
                "&info_format=application/json" +
                "&transparent=true" +
                "&feature_count=50";

What I got done so far (I think) is to calculate the x and y position of where I clicked from the latitute, longitude and the zoom level.

private void getFeatureInfo(LatLng latLng) {
    // get current zoom
    int zoom = (int)mMap.getCameraPosition().zoom;

    // get "click" point coordinates in pixels
    long pointNorthWestX = lonToX(latLng.longitude, zoom);
    long pointNorthWestY = latToY(latLng.latitude, zoom);
}

public static long lonToX(double lon, int zoom) {
    int offset = 256 << (zoom - 1);
    return (int)Math.floor(offset + (offset * lon / 180));
}

public static long latToY(double lat, int zoom) {
    int offset = 256 << (zoom - 1);
    return (int)Math.floor(offset - offset / Math.PI * Math.log((1 + Math.sin(Math.toRadians(lat))) / (1 - Math.sin(Math.toRadians(lat)))) / 2);
}

Unfortunately, these values seem to be way off in comparison of what I get when I click the same position in the layer preview on geoserver.

Does anyone know what the necessary calculation steps are to get the feature information at the position I clicked at from the latitude, longitude and zoom level? I'd be really thankful for any help with this.

A getFeatureRequest is, essentially, the getMap request you used to fetch the map you are querying with some parameters added. You must add QUERY_LAYERS which is the names of the layers you want information about, and X & Y (or I & J in version 1.3.0) which are the location of the pixel you want information about (so where you clicked). Optionally, you can add an info_format to control the returned format.

For further details I suggest you read the standard document .

I have found the solution for this and it was way easier than I thought. The geoserver already supports projection transformations from the LatLon value that I get from clicking on the map to the XY-Coordinates that are needed for the getFeatureInfo() request.

I have change the code to the following.

private void getFeatureInfo(LatLng latLng) {
    String url = "http://xxxxxxxxxx.at/geoserver/wms" +
            "?service=WMS" +
            "&version=1.1.1" +
            "&request=GetFeatureInfo" +
            "&layers=satgrass:INSPIRE_SCHLAEGE_2020_POLYGON" +
            "&query_layers=satgrass:INSPIRE_SCHLAEGE_2020_POLYGON" +
            "&exceptions=application/vnd.ogc.se_inimage" +
            "&x=128" +
            "&y=128" +
            "&bbox=" + (latLng.longitude - 0.0000000001) + "," + (latLng.latitude - 0.0000000001) + "," + (latLng.longitude + 0.0000000001) + "," + (latLng.latitude + 0.0000000001) +
            "&width=256" +
            "&height=256" +
            "&srs=EPSG:4326" +
            "&format=image/png" +
            "&info_format=application/json" +
            "&transparent=true" +
            "&feature_count=50";
}

The width and height are both 256 , which I have initially set in the TileProvider. The x and y values are both 128 , which would be the center of each tile.

What I basically needed to do now is to create a bounding box in which the geoserver will look for features. I made that as small as possible (should be in cm or mm range) so only ever 1 feature is returned. For that, I'm distracting 0.0000000001 from the minX and minY values and adding 0.0000000001 to the maxX and maxY values.

The important part was to use EPSG:4326 for this request. The geoserver will then automatically calculate the projection for the EPSG:31287 layer and return the feature information at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM