Geotools矢量网格图层无法显示问题求助
问题
我正在开发基于Geotools的地图应用,近期网格图层莫名无法显示。现有代表地球的WMS地图,以及采用WGS84地理坐标系、10°方格的FeatureLayer网格。以下是初始化MapContent的代码,参照Geotools矢量网格教程设置了澳大利亚区域的网格:
public void initMap(int w, int h) { mapContent = new MapContent(); mapContent.getViewport().setMatchingAspectRatio(true); mapContent.getViewport().setFixedBoundsOnResize(false); // WMS wmsLayer = new WMSLayer(wmsConnection, selectedLayer); ReferencedEnvelope dataBounds = wmsLayer.getBounds(); CoordinateReferenceSystem dataCrs = dataBounds.getCoordinateReferenceSystem(); mapContent.addLayer(wmsLayer); mapContent.getViewport().setScreenArea(new Rectangle(w, h)); mapContent.getViewport().setBounds(dataBounds); mapContent.getViewport().setCoordinateReferenceSystem(dataCrs); // Grid ReferencedEnvelope gridBounds = new ReferencedEnvelope(110, 150, -45, -8, DefaultGeographicCRS.WGS84); FeatureSource<SimpleFeatureType, SimpleFeature> lonLatGrid = Grids.createSquareGrid(gridBounds, 10); ReprojectingFeatureCollection rfc; try { SimpleFeatureCollection sfc = (SimpleFeatureCollection)lonLatGrid.getFeatures(); rfc = new ReprojectingFeatureCollection(sfc, mapContent.getViewport().getCoordinateReferenceSystem()); } catch (IOException e) { throw new RuntimeException(e); } Style style = SLD.createSimpleStyle(rfc.getSchema()); FeatureLayer grid = new FeatureLayer(rfc, style); mapContent.addLayer(grid); }
我使用StreamingRenderer在JavaFX Canvas上渲染地图,WMS图层可正常显示,但FeatureLayer网格完全不显示。相关Geotools日志如下:
2023-02-16 14:50:26.739 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : Computed scale denominator: 7.687380703318137E7 2023-02-16 14:50:26.740 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : creating rules for scale denominator - 76,873,807.033 2023-02-16 14:50:26.740 DEBUG 133118 --- [lication Thread] org.geotools.styling : creating defaultMark 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.styling : creating defaultMark 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : CRSConverterFactory can be applied from Strings to CRS only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : InterpolationConverterFactory can be applied from Strings to Interpolation only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : CRSConverterFactory can be applied from Strings to CRS only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : InterpolationConverterFactory can be applied from Strings to Interpolation only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : CRSConverterFactory can be applied from Strings to CRS only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.data.util : InterpolationConverterFactory can be applied from Strings to Interpolation only. 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : Processing 1 stylers for http://www.opengis.net/gml:grid 2023-02-16 14:50:26.741 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : Expanding rendering area by 2 pixels to consider stroke width 2023-02-16 14:50:26.742 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : Querying layer http://www.opengis.net/gml:grid with bbox: ReferencedEnvelope[-139.28905476171872 : 108.9843689453125, -60.03515721679681 : 107.02733350195317] 2023-02-16 14:50:26.743 DEBUG 133118 --- [lication Thread] org.geotools.styling : creating defaultMark 2023-02-16 14:50:26.765 DEBUG 133118 --- [lication Thread] org.geotools.renderer.label : TOTAL LINE LABELS : 0 2023-02-16 14:50:26.765 DEBUG 133118 --- [lication Thread] org.geotools.renderer.label : PAINTED LINE LABELS : 0 2023-02-16 14:50:26.765 DEBUG 133118 --- [lication Thread] org.geotools.renderer.label : REMAINING LINE LABELS : 0 2023-02-16 14:50:26.765 DEBUG 133118 --- [lication Thread] org.geotools.renderer.lite : Style cache hit ratio: 0.9977777777777778 , hits 449, requests 450
解决方案
验证投影后网格与视口的重叠性:
从日志查询的bbox范围来看,投影后的网格可能不在当前视口内。添加代码验证:// 创建rfc后添加 ReferencedEnvelope reprojectedBounds = rfc.getBounds(); System.out.println("投影后网格范围: " + reprojectedBounds); System.out.println("视口范围: " + mapContent.getViewport().getBounds()); System.out.println("是否重叠: " + reprojectedBounds.intersects(mapContent.getViewport().getBounds()));若不重叠,可先将网格原始范围转换为WMS的CRS,再创建网格:
ReferencedEnvelope gridBoundsWGS84 = new ReferencedEnvelope(110, 150, -45, -8, DefaultGeographicCRS.WGS84); ReferencedEnvelope gridBoundsWMS = gridBoundsWGS84.transform(dataCrs, true); // 这里网格大小用对应CRS的单位,比如米 FeatureSource<SimpleFeatureType, SimpleFeature> grid = Grids.createSquareGrid(gridBoundsWMS, 100000);替换默认网格样式:
SLD.createSimpleStyle生成的默认样式可能线条颜色与WMS背景重合,或线条宽度为0,手动指定明显样式:Style style = SLD.createLineStyle(Color.RED, 2.0f);检查图层遮挡与顺序:
暂时移除WMS图层,单独测试网格是否显示;同时确认网格图层添加在WMS之后,避免被完全遮挡。确认视口CRS正确性:
打印WMS返回的CRS信息,验证是否正确获取:System.out.println("WMS坐标系: " + dataCrs.getName().getCode());
内容的提问来源于stack exchange,提问作者Elyas Tigre
相关产品推荐
相关产品推荐

