Skip to content

Commit

Permalink
bugfix wms version for rasterdb and postgis layers to correct axis or…
Browse files Browse the repository at this point in the history
…der; bugfix vectordb get epsg from SpatialReference
  • Loading branch information
swoellauer committed Mar 19, 2024
1 parent c4a94bf commit 0100c53
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 50 deletions.
4 changes: 2 additions & 2 deletions app/src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ export default defineComponent({
this.sessionCnt++;
image.getImage().src = srcUrl;
},
params: {},
params: { VERSION: "1.1.1" },
}),
});
this.refreshPostgisLayerParameters(layerEntry);
Expand Down Expand Up @@ -858,7 +858,7 @@ export default defineComponent({
this.sessionCnt++;
image.getImage().src = srcUrl;
},
params: {},
params: { VERSION: "1.1.1" },
}),
});
this.refreshRasterdbLayerParameters(layerEntry);
Expand Down
4 changes: 4 additions & 0 deletions src/pointcloud/Rect2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,8 @@ public Point2d toCenterPoint() {
public Point2d toMinPoint() {
return new Point2d(xmin, ymin);
}

public Rect2d toSwappedAxes() {
return new Rect2d(this.ymin, this.xmin, this.ymax, this.xmax);
}
}
27 changes: 23 additions & 4 deletions src/server/api/postgis/PostgisHandler_wms.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private void handle_GetCapabilities(PostgisLayer postgisLayer, Request request,

private Element xmlGetCapabilities(PostgisLayer postgisLayer, Document doc, String requestUrl) {
Element rootElement = doc.createElementNS("http://www.opengis.net/wms", "WMS_Capabilities");
rootElement.setAttribute("version", "1.3.0");
//rootElement.setAttribute("version", "1.3.0");
rootElement.setAttribute("version", "1.1.1"); // workaround for swapped axis order in some projections in WMS 1.3.0 but not in WMS 1.1.1
rootElement.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

Element eService = XmlUtil.addElement(rootElement, "Service");
Expand Down Expand Up @@ -291,7 +292,13 @@ private void handle_GetMap(PostgisLayer postgisLayer, Request request, Response
}

int wmsEPSG = 0;
String crs = Web.getString(request, "CRS");
String crs = Web.getString(request, "CRS", null);
if(crs == null) {
crs = Web.getString(request, "SRS", null);
}
/*if(crs == null) {
throw new RuntimeException("parameter not found: CRS or SRS");
}*/
if(crs != null) {
if(crs.startsWith("EPSG:")) {
wmsEPSG = Integer.parseInt(crs.substring(5));
Expand Down Expand Up @@ -345,8 +352,20 @@ private void handle_GetFeatureInfo(PostgisLayer postgisLayer, Request request, R
}
int reqWidth = Web.getInt(request, "WIDTH");
int reqHeight = Web.getInt(request, "HEIGHT");
int reqX = Web.getInt(request, "I");
int reqY = Web.getInt(request, "J");
int reqX = Web.getInt(request, "I", -999);
if(reqX == -999) {
reqX = Web.getInt(request, "X", -999);
}
if(reqX == -999) {
throw new RuntimeException("parameter not found: I or X");
}
int reqY = Web.getInt(request, "J", -999);
if(reqY == -999) {
reqY = Web.getInt(request, "Y", -999);
}
if(reqY == -999) {
throw new RuntimeException("parameter not found: J or Y");
}

double xres = wmsRect2d.width() / reqWidth;
double yres = wmsRect2d.height() / reqHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public static Element addElement(Element root, String name, String textContent)

private static Node getCapabilities(RasterDB rasterdb, CustomWMS customWMS, String requestUrl, Document doc) {
Element rootElement = doc.createElementNS(NS_URL, "WMS_Capabilities");
rootElement.setAttribute("version", "1.3.0");
//rootElement.setAttribute("version", "1.3.0");
rootElement.setAttribute("version", "1.1.1"); // workaround for swapped axis order in some projections in WMS 1.3.0 but not in WMS 1.1.1
rootElement.setAttribute("xmlns:xlink", NS_XLINK);

Element eService = addElement(rootElement, "Service");
Expand Down
52 changes: 34 additions & 18 deletions src/server/api/rasterdb/RasterdbMethod_wms_GetFeatureInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.UserIdentity;
import org.gdal.osr.SpatialReference;
import org.tinylog.Logger;

import broker.TimeSlice;
Expand Down Expand Up @@ -41,27 +40,44 @@ public class RasterdbMethod_wms_GetFeatureInfo {

public static void handle_GetFeatureInfo(RasterDB rasterdb, Request request, Response response, UserIdentity userIdentity) throws IOException {
int wmsEPSG = 0;
String crs = Web.getString(request, "CRS");
if(crs != null) {
if(crs.startsWith("EPSG:")) {
wmsEPSG = Integer.parseInt(crs.substring(5));
} else {
throw new RuntimeException("unknown CRS");
}
}
String crs = Web.getString(request, "CRS", null);
if(crs == null) {
crs = Web.getString(request, "SRS", null);
}
if(crs == null) {
throw new RuntimeException("parameter not found: CRS or SRS");
}
if(crs.startsWith("EPSG:")) {
wmsEPSG = Integer.parseInt(crs.substring(5));
} else {
throw new RuntimeException("unknown CRS");
}
int layerEPSG = rasterdb.ref().getEPSG(0);
boolean reproject = wmsEPSG > 0 && layerEPSG > 0 && wmsEPSG != layerEPSG;

String bboxParam = request.getParameter("BBOX");
Rect2d rect2d = null;
if(bboxParam != null) {
String[] bbox = bboxParam.split(",");
rect2d = Rect2d.parseBbox(bbox);
}
int reqWidth = Web.getInt(request, "WIDTH");
int reqHeight = Web.getInt(request, "HEIGHT");
int reqX = Web.getInt(request, "I");
int reqY = Web.getInt(request, "J");
int reqHeight = Web.getInt(request, "HEIGHT");

int reqX = Web.getInt(request, "I", -999);
if(reqX == -999) {
reqX = Web.getInt(request, "X", -999);
}
if(reqX == -999) {
throw new RuntimeException("parameter not found: I or X");
}
int reqY = Web.getInt(request, "J", -999);
if(reqY == -999) {
reqY = Web.getInt(request, "Y", -999);
}
if(reqY == -999) {
throw new RuntimeException("parameter not found: J or Y");
}

double xres = rect2d.width() / reqWidth;
double yres = rect2d.height() / reqHeight;
Expand All @@ -71,7 +87,7 @@ public static void handle_GetFeatureInfo(RasterDB rasterdb, Request request, Res
Logger.info(reqWidth + " " + reqHeight + " " + reqX + " " + reqY);
Logger.info(rect2d);
Logger.info(wmsPixelRect2d);

Rect2d layerPixelRect2d = wmsPixelRect2d;
if(reproject) {
util.GeoUtil.Transformer transformer = GeoUtil.getCoordinateTransformer(wmsEPSG, layerEPSG);
Expand Down Expand Up @@ -209,16 +225,16 @@ private static void xmlGetFeatureStream(RasterDB rasterdb, HashMap<String, Objec

xmlWriter.writeStartElement("gml:featureMember");
xmlWriter.writeStartElement("pixel");

//String escapedFieldValue = XmlUtil.encodeXML(ctx.get("values").toString());
xmlWriter.writeStartElement("value");
xmlWriter.writeCharacters(ctx.get("values").toString());
xmlWriter.writeEndElement(); // fieldName

GeoReference ref = rasterdb.ref();
int layerEPSG = ref.getEPSG(0);
boolean hasLayerCRS = layerEPSG > 0;

xmlWriter.writeStartElement("geometry"); // geometry after properties for better properties reading by qgis
xmlWriter.writeStartElement("gml:MultiSurface");
if(hasLayerCRS) {
Expand All @@ -229,7 +245,7 @@ private static void xmlGetFeatureStream(RasterDB rasterdb, HashMap<String, Objec
xmlWriter.writeStartElement("gml:exterior");
xmlWriter.writeStartElement("gml:LinearRing");
xmlWriter.writeStartElement("gml:posList");

Rect2d pixelRect2d = (Rect2d) ctx.get("centerPixelrect");
String s = pixelRect2d.xmin + " " + pixelRect2d.ymax + " " +
pixelRect2d.xmax + " " + pixelRect2d.ymax + " " +
Expand Down
6 changes: 6 additions & 0 deletions src/server/api/rasterdb/RasterdbMethod_wms_GetMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public static void handle_GetMap(RasterDB rasterdb, String target, Request reque
int layerEPSG = -1;
int wmsEPSG = -1;
String crsParameter = Web.getString(request, "CRS", null);
if(crsParameter == null) {
crsParameter = Web.getString(request, "SRS", null);
}
/*if(crsParameter == null) {
throw new RuntimeException("parameter not found: CRS or SRS");
}*/
if(crsParameter != null) {
GeoReference ref = rasterdb.ref();
if(ref.has_code()) {
Expand Down
1 change: 0 additions & 1 deletion src/server/api/rasterdb/TimeFrameReprojector.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.osr.CoordinateTransformation;
import org.tinylog.Logger;

import pointcloud.Rect2d;
Expand Down
81 changes: 59 additions & 22 deletions src/server/api/vectordbs/VectordbHandler_wms.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.IOException;
Expand Down Expand Up @@ -33,8 +32,6 @@
import broker.Broker;
import jakarta.servlet.http.HttpServletResponse;
import pointcloud.Rect2d;
import postgis.PostgisLayer;
import postgis.style.StyleProvider;
import server.api.postgis.PostgisHandler_wms;
import util.GeoUtil;
import util.Interruptor;
Expand Down Expand Up @@ -92,6 +89,8 @@ public void handleGET(VectorDB vectordb, String target, Request request, Respons

public void handle_GetMap(VectorDB vectordb, Request request, Response response, UserIdentity userIdentity) throws IOException {
try {
String version = Web.getString(request, "VERSION", "1.1.1");
boolean version130 = version.equals("1.3.0");
int width = Web.getInt(request, "WIDTH");
int height = Web.getInt(request, "HEIGHT");
String crs = Web.getString(request, "CRS", null);
Expand Down Expand Up @@ -129,7 +128,27 @@ public void handle_GetMap(VectorDB vectordb, Request request, Response response,
}

String[] bbox = request.getParameter("BBOX").split(",");
Rect2d wmsRect = Rect2d.parse(bbox[0], bbox[1], bbox[2], bbox[3]);
Rect2d wmsRect = Rect2d.parse(bbox[0], bbox[1], bbox[2], bbox[3]);
/*if(version130) {
try {
Logger.info(crs);
CRSFactory crsFactory = new CRSFactory();
CoordinateReferenceSystem proj4Crs = crsFactory.createFromName("EPSG:4326");
//CoordinateReferenceSystem proj4Crs = crsFactory.createFromName(crs);
Logger.info(proj4Crs.getParameterString());
Projection proj4Projection = proj4Crs.getProjection();
Logger.info(proj4Projection.getEPSGCode());
AxisOrder proj4AxisOrder = proj4Projection.getAxisOrder();
if(!AxisOrder.ENU.equals(proj4AxisOrder)) {
Logger.info("toSwappedAxes");
wmsRect = wmsRect.toSwappedAxes();
} else {
Logger.info("ENU");
}
} catch(Exception e) {
Logger.info(e);
}
}*/

ImageBufferARGB image = null;
DataSource datasource = vectordb.getDataSource();
Expand All @@ -144,17 +163,17 @@ public void handle_GetMap(VectorDB vectordb, Request request, Response response,
if(layerWmsTransformer == null) {
if(layerSr != null) {
swapCoordinates = layerSr.GetAxisOrientation(null, 0) == 1;
// if(GeoUtil.WGS84_SPATIAL_REFERENCE.IsSame(layerSr) != 0) { // workaround for swapped axis but not in GetAxisOrientation for EPSG:4326
// swapCoordinates = true;
// } else {
// swapCoordinates = layerSr.GetAxisOrientation(null, 0) == 1;
// }
// if(GeoUtil.WGS84_SPATIAL_REFERENCE.IsSame(layerSr) != 0) { // workaround for swapped axis but not in GetAxisOrientation for EPSG:4326
// swapCoordinates = true;
// } else {
// swapCoordinates = layerSr.GetAxisOrientation(null, 0) == 1;
// }
}
}
Logger.info("layerSr.GetAxisOrientation(null, 0) " + layerSr.GetAxisOrientation(null, 0) + " " + swapCoordinates);
image = ConverterRenderer.render(datasource, vectordb, wmsRect, width, height, labelField, style, layerWmsTransformer, swapCoordinates);
//image = ConverterRenderer.render(datasource, vectordb, wmsRect, width, height, labelField, style, layerWmsTransformer, false);

printCRSinfo(3044);
printCRSinfo(3857);
printCRSinfo(4326);
Expand Down Expand Up @@ -196,7 +215,7 @@ public void handle_GetMap(VectorDB vectordb, Request request, Response response,
Logger.warn(e);
}
}

private static void printCRSinfo(int epsg) {
SpatialReference sr = GeoUtil.getSpatialReferenceFromEPSG(epsg);
Logger.info(epsg + " AxisOrientation " + sr.GetAxisOrientation(null, 0));
Expand Down Expand Up @@ -229,7 +248,8 @@ private void xml_root_GetCapabilities(VectorDB vectordb, PrintWriter out, String

private Node getCapabilities(VectorDB vectordb, Document doc, String requestUrl) {
Element rootElement = doc.createElementNS("http://www.opengis.net/wms", "WMS_Capabilities");
rootElement.setAttribute("version", "1.3.0");
//rootElement.setAttribute("version", "1.3.0");
rootElement.setAttribute("version", "1.1.1"); // workaround for swapped axis order in some projections in WMS 1.3.0 but not in WMS 1.1.1
rootElement.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

Element eService = XmlUtil.addElement(rootElement, "Service");
Expand Down Expand Up @@ -325,13 +345,17 @@ private static void addStyle(VectorDB vectordb, Element eRootLayer, String reque

public void handle_GetFeatureInfo(VectorDB vectordb, Request request, Response response, UserIdentity userIdentity) throws IOException {
int wmsEPSG = 0;
String crs = Web.getString(request, "CRS");
if(crs != null) {
if(crs.startsWith("EPSG:")) {
wmsEPSG = Integer.parseInt(crs.substring(5));
} else {
throw new RuntimeException("unknown CRS");
}
String crs = Web.getString(request, "CRS", null);
if(crs == null) {
crs = Web.getString(request, "SRS", null);
}
if(crs == null) {
throw new RuntimeException("parameter not found: CRS or SRS");
}
if(crs.startsWith("EPSG:")) {
wmsEPSG = Integer.parseInt(crs.substring(5));
} else {
throw new RuntimeException("unknown CRS");
}

int layerEPSG = 0;
Expand All @@ -350,13 +374,26 @@ public void handle_GetFeatureInfo(VectorDB vectordb, Request request, Response r
}
int reqWidth = Web.getInt(request, "WIDTH");
int reqHeight = Web.getInt(request, "HEIGHT");
int reqX = Web.getInt(request, "I");
int reqY = Web.getInt(request, "J");
int reqX = Web.getInt(request, "I", -999);
if(reqX == -999) {
reqX = Web.getInt(request, "X", -999);
}
if(reqX == -999) {
throw new RuntimeException("parameter not found: I or X");
}
int reqY = Web.getInt(request, "J", -999);
if(reqY == -999) {
reqY = Web.getInt(request, "Y", -999);
}
if(reqY == -999) {
throw new RuntimeException("parameter not found: J or Y");
}

double xres = wmsRect2d.width() / reqWidth;
double yres = wmsRect2d.height() / reqHeight;

Rect2d wmsPixelRect2d = new Rect2d(wmsRect2d.xmin + xres * reqX, wmsRect2d.ymax - yres * (reqY + 1), wmsRect2d.xmin + xres * (reqX + 1), wmsRect2d.ymax - yres * reqY);
//Rect2d wmsPixelRect2d = new Rect2d(wmsRect2d.xmin + xres * reqX, wmsRect2d.ymax - yres * (reqY + 1), wmsRect2d.xmin + xres * (reqX + 1), wmsRect2d.ymax - yres * reqY);
Rect2d wmsPixelRect2d = new Rect2d(wmsRect2d.xmin + xres * (reqX - 4), wmsRect2d.ymax - yres * (reqY + 5), wmsRect2d.xmin + xres * (reqX + 5), wmsRect2d.ymax - yres * (reqY - 4));

Logger.info(reqWidth + " " + reqHeight + " " + reqX + " " + reqY);
Logger.info(wmsRect2d);
Expand Down
12 changes: 10 additions & 2 deletions src/vectordb/VectorDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ public void refreshDatatag() {
}

public VectordbDetails getDetails() {
//Logger.info("|getDetails()| " + getName());
VectordbDetails details = new VectordbDetails();
try {
DataSource datasource = getDataSource();
Expand All @@ -726,6 +727,11 @@ public VectordbDetails getDetails() {
if(proj4 != null) {
details.proj4 = proj4;
}
if("EPSG".equals(layerRef.GetAuthorityName("PROJCS"))) {
String layerEPSG = layerRef.GetAuthorityCode("PROJCS");
details.epsg = layerEPSG == null || layerEPSG.isBlank() ? "" : layerEPSG;
//Logger.info(details.epsg);
}
FeatureDefn featureDfn = layer.GetLayerDefn();
int fieldCount = featureDfn.GetFieldCount();
for (int i = 0; i < fieldCount; i++) {
Expand All @@ -735,10 +741,12 @@ public VectordbDetails getDetails() {
}
}
}
if(!details.proj4.isEmpty()) {
if((details.epsg == null || details.epsg.isBlank()) && !details.proj4.isEmpty()) {
String epsg = GeoUtil.CRS_FACTORY.readEpsgFromParameters(details.proj4);
details.epsg = epsg == null ? "" : epsg;
details.epsg = epsg == null || epsg.isBlank() ? "" : epsg;
//Logger.info(details.epsg);
}
//Logger.info("|" + details.proj4 + "|");
details.attributes = attributes.readonlyWeakView();
} finally {
closeDataSource(datasource);
Expand Down

0 comments on commit 0100c53

Please sign in to comment.