GeoTools开发WMS:EPSG4326请求与EPSG2383高斯投影瓦片偏移问题求助
高斯投影(EPSG:2383)数据源适配EPSG:4326 WMS请求的瓦片偏移问题
使用GeoTools开发WMS服务,前端请求采用EPSG:4326坐标系,PostGIS数据源存储的是高斯投影坐标(EPSG:2383)。尝试将前端传入的bbox矩形区域从EPSG:4326转换为EPSG:2383后进行视口映射,当瓦片固定为256*256规格时,出现瓦片拼接偏移、线条扭曲的问题。将所有PostGIS数据源转换为EPSG:3857后,以EPSG:3857的bbox请求无偏移,但全量转换数据源的方案不可取。先后尝试自定义坐标转换(含墨卡托转经纬度、四参数转换)及GeoTools原生转换工具,均出现上述问题,寻求无需转换全部数据源的解决办法。
尝试过的代码实现
自定义坐标转换核心代码
double x1 = getMapDTO.getBbox()[0]; double y1 = getMapDTO.getBbox()[1]; double x2 = getMapDTO.getBbox()[2]; double y2 = getMapDTO.getBbox()[3]; // 先将4326经纬度转为3857墨卡托坐标,再通过四参数转换为2383平面坐标 Point1 = point1.transformMercatorToXy(2383); Point2 = point2.transformMercatorToXy(2383); x1 = wpgPoint1.getX(); y1 = wpgPoint1.getY(); x2 = wpgPoint2.getX(); y2 = wpgPoint2.getY(); Double[] box = new Double[]{x1, y1, x2, y2}; getMapDTO.setBbox(box); mapArea = new ReferencedEnvelope(x1, x2, y1, y2, CRS.decode("EPSG:2383")); map.getViewport().setBounds(new ReferencedEnvelope(mapArea, CRS.decode("EPSG:2383"))); sr.setMapContent(map); // 初始化输出图像,width = 256, height=256 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); bi = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); g.dispose();
墨卡托转经纬度方法
public static double[] mercator2LonLat(double[] lonLat) { double x = lonLat[0] / 20037508.34 * 180; double y = lonLat[1] / 20037508.34 * 180; y = (180 / Math.PI) * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2); return new double[]{x, y}; }
四参数转换方法
private double[] fourParameterConversion(double x, double y) { //四参数指两个坐标参考系间的X轴偏移、Y轴偏移、旋转角、缩放比例 FourParam fourParam = transformParam.getFourParam(); double x_off = fourParam.getDx(), y_off = fourParam.getDy(), t = fourParam.getRotate(), m = fourParam.getScale(); double x1 = x_off + (y * Math.cos(t) - x * Math.sin(t)) * m; double y1 = y_off + (x * Math.cos(t) + y * Math.sin(t)) * m; double[] xy = new double[]{Math.round(x1 * ROUNDING_FACTOR) / ROUNDING_FACTOR, Math.round(y1 * ROUNDING_FACTOR) / ROUNDING_FACTOR}; return new double[]{xy[1], xy[0]}; }
GeoTools原生转换代码
CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326"); ReferencedEnvelope envelope = new ReferencedEnvelope(x1, x2, y1, y2, sourceCRS); // 使用 envelope 周围的10个采样点进行转换 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:2383"); ReferencedEnvelope result = envelope.transform(targetCRS, true, 10); map.getViewport().setBounds(result); sr.setMapContent(map);
内容的提问来源于stack exchange,提问作者threecat
相关产品推荐
相关产品推荐

