[英]Getting Layer Feature Information from GeoServer (Google Maps)
我正在使用 Android Studio(使用 Java)編寫 Android 應用程序。 該應用程序使用谷歌地圖,並有一個圖層,其中包含從地理服務器獲取的字段所有權信息。 設置它的代碼如下,並且運行良好。
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;
}
}
上面的代碼將在 Google 地圖上生成以下疊加層。 在地理服務器上,圖層保存為 EPSG:31287 (Austria Lambert)。
我現在想做的是在單擊其中一個字段時獲取功能信息。 我嘗試了很多,但無法弄清楚如何計算必要的值(寬度、高度、x、y、bbox)以從我單擊的緯度和經度中獲取特征信息。 具體來說,這些是我缺少的查詢中的值。
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";
到目前為止我所做的(我認為)是計算我從緯度、經度和縮放級別單擊的位置的 x 和 y position。
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);
}
不幸的是,與我在 geoserver 的圖層預覽中單擊相同的 position 時得到的值相比,這些值似乎相差甚遠。
有誰知道我從緯度、經度和縮放級別單擊的 position 獲取特征信息的必要計算步驟是什么? 我將非常感謝您對此的任何幫助。
本質上, getFeatureRequest
是您用來獲取您正在查詢的 map 的getMap
請求,並添加了一些參數。 您必須添加QUERY_LAYERS
,這是您想要了解信息的圖層的名稱,以及X
和Y
(或版本 1.3.0 中的I
和J
),它們是您想要了解信息的像素的位置(因此您單擊的位置)。 或者,您可以添加info_format
來控制返回的格式。
有關更多詳細信息,我建議您閱讀標准文檔。
我已經找到了解決方案,它比我想象的要容易得多。 地理服務器已經支持從我通過單擊 map 獲得的 LatLon 值到getFeatureInfo()
請求所需的 XY 坐標的投影轉換。
我已將代碼更改為以下內容。
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";
}
width
和height
都是256
,這是我最初在 TileProvider 中設置的。 x
和y
值都是128
,這將是每個圖塊的中心。
我現在基本上需要做的是創建一個邊界框,地理服務器將在其中查找特征。 我把它做得盡可能小(應該在厘米或毫米范圍內),所以只返回 1 個特征。 為此,我將0.0000000001
從minX
和minY
值中分心,並將0.0000000001
添加到maxX
和maxY
值。
重要的部分是使用EPSG:4326
這個請求。 然后,地理服務器將自動計算EPSG:31287
層的投影並返回該點的特征信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.