GeoTools-Swing显示原理

本文详细介绍了如何使用GeoTools库在Swing环境中显示地图,涵盖了创建图层、地图Model、地图框架JMapFrame的步骤,以及JMapPane、MapLayerTable、JMapFrame的实现原理和方法。通过示例代码展示了如何加载地图文件、控制图层显示,并提供了文件选择、字体选择和样式选择的常用对话框操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GeoTools-Swing显示原理

sf2gis@163.com

2016年1月29日

2016年2月3日修正JMapPane继承层次

 

1  目标:GeoTools的GUI显示,验证GeoTools功能,示例教学。

一般工程中不推荐使用。

2 原理:基于Swing进行显示、交互。

3 流程:创建图层(打开地图文件,获取要素数据源,创建要素样式)。创建地图Model(图层和地图UI),创建地图框架(地图和UI),配置框架组件。

3.1 创建图层:打开地图文件,获取要素数据源,创建要素样式。

           FileDataStore ds =FileDataStoreFinder.getDataStore(newURL("file://D:/Data/shapefiles/states.shp"));

           SimpleFeatureStore sfs =(SimpleFeatureStore) ds.getFeatureSource();

          

           Style style =SLD.createSimpleStyle(sfs.getSchema());

           Layer layer = newFeatureLayer(sfs,style);

3.2 创建地图Model(图层和地图UI)

           MapContent map = new MapContent();

           map.setTitle("GtDemoXX");

           map.addLayer(layer);

3.3 创建地图框架(地图和UI),配置框架组件

           JMapFrame mf = new JMapFrame(map);

           mf.setBounds(0, 0, 800, 600);

           mf.enableLayerTable(true);

 &

org.geotools org.geotools.arcsde org.geotools.arcsde.data org.geotools.arcsde.data.versioning org.geotools.arcsde.data.view org.geotools.arcsde.filter org.geotools.arcsde.gce org.geotools.arcsde.gce.band org.geotools.arcsde.gce.imageio org.geotools.arcsde.gce.producer org.geotools.arcsde.pool org.geotools.axis org.geotools.brewer.color org.geotools.coverage org.geotools.coverage.grid org.geotools.coverage.grid.io org.geotools.coverage.grid.io.imageio org.geotools.coverage.io org.geotools.coverage.processing org.geotools.coverage.processing.operation org.geotools.data org.geotools.data.collection org.geotools.data.crs org.geotools.data.db2 org.geotools.data.db2.filter org.geotools.data.dir org.geotools.data.gml org.geotools.data.gpx org.geotools.data.gpx.temporal org.geotools.data.h2 org.geotools.data.jdbc org.geotools.data.jdbc.attributeio org.geotools.data.jdbc.datasource org.geotools.data.jdbc.fidmapper org.geotools.data.jdbc.referencing org.geotools.data.memory org.geotools.data.mif org.geotools.data.mysql org.geotools.data.oracle org.geotools.data.oracle.attributeio org.geotools.data.oracle.referencing org.geotools.data.oracle.sdo org.geotools.data.ows org.geotools.data.postgis org.geotools.data.postgis.attributeio org.geotools.data.postgis.collection org.geotools.data.postgis.fidmapper org.geotools.data.postgis.referencing org.geotools.data.property org.geotools.data.shapefile org.geotools.data.shapefile.dbf org.geotools.data.shapefile.indexed org.geotools.data.shapefile.indexed.attribute org.geotools.data.shapefile.prj org.geotools.data.shapefile.shp org.geotools.data.shapefile.shp.xml org.geotools.data.store org.geotools.data.tiger org.geotools.data.view org.geotools.data.vpf org.geotools.data.vpf.exc org.geotools.data.vpf.file org.geotools.data.vpf.ifc org.geotools.data.vpf.io org.geotools.data.vpf.readers org.geotools.data.vpf.util org.geotools.data.wfs org.geotools.data.wms org.geotools.data.wms.request org.geotools.data.wms.response org.geotools.data.wms.xml org.geotools.demo org.geotools.demo.data org.geotools.demo.example org.geotools.demo.features org.geotools.demo.geometry org.geotools.demo.introduction org.geotools.demo.jts org.geotools.demo.libraryJTS org.geotools.demo.main org.geotools.demo.mappane org.geotools.demo.metadata.example org.geotools.demo.postgis org.geotools.demo.swing org.geotools.demo.swing.process org.geotools.demo.widgets org.geotools.demo.xml org.geotools.display.canvas org.geotools.display.canvas.map org.geotools.display.event org.geotools.display.geom org.geotools.display.style org.geotools.factory org.geotools.feature org.geotools.feature.collection org.geotools.feature.simple org.geotools.feature.type org.geotools.feature.visitor org.geotools.filter org.geotools.filter.capability org.geotools.filter.expression org.geotools.filter.function org.geotools.filter.function.math org.geotools.filter.identity org.geotools.filter.parser org.geotools.filter.spatial org.geotools.filter.text.cql2 org.geotools.filter.text.txt org.geotools.filter.v1_0 org.geotools.filter.v1_0.capabilities org.geotools.filter.v1_1 org.geotools.filter.v1_1.capabilities org.geotools.filter.visitor org.geotools.gce.arcgrid org.geotools.gce.geotiff org.geotools.gce.geotiff.crs_adapters org.geotools.gce.geotiff.IIOMetadataAdpaters org.geotools.gce.geotiff.IIOMetadataAdpaters.utils org.geotools.gce.geotiff.IIOMetadataAdpaters.utils.codes org.geotools.gce.gtopo30 org.geotools.gce.image org.geotools.gce.imagemosaic org.geotools.gce.imagepyramid org.geotools.geometry org.geotools.geometry.array org.geotools.geometry.coordinatesequence org.geotools.geometry.iso org.geotools.geometry.iso.aggregate org.geotools.geometry.iso.complex org.geotools.geometry.iso.coordinate org.geotools.geometry.iso.index org.geotools.geometry.iso.index.quadtree org.geotools.geometry.iso.io org.geotools.geometry.iso.io.wkt org.geotools.geometry.iso.operation org.geotools.geometry.iso.operation.overlay org.geotools.geometry.iso.operation.relate org.geotools.geometry.iso.primitive org.geotools.geometry.iso.root org.geotools.geometry.iso.topograph2D org.geotools.geometry.iso.topograph2D.index org.geotools.geometry.iso.topograph2D.util org.geotools.geometry.iso.util org.geotools.geometry.iso.util.algorithm2D org.geotools.geometry.iso.util.algorithmND org.geotools.geometry.iso.util.elem2D org.geotools.geometry.iso.util.interpolation org.geotools.geometry.iso.util.topology org.geotools.geometry.jts org.geotools.geometry.jts.coordinatesequence org.geotools.geometry.jts.spatialschema org.geotools.geometry.jts.spatialschema.geometry org.geotools.geometry.jts.spatialschema.geometry.aggregate org.geotools.geometry.jts.spatialschema.geometry.complex org.geotools.geometry.jts.spatialschema.geometry.geometry org.geotools.geometry.jts.spatialschema.geometry.primitive org.geotools.geometry.text org.geotools.gml org.geotools.gml.producer org.geotools.gml2 org.geotools.gml2.bindings org.geotools.gml3 org.geotools.gml3.bindings org.geotools.gml3.bindings.smil org.geotools.gml3.smil org.geotools.gpx org.geotools.gpx.bean org.geotools.gpx.binding org.geotools.graph.build org.geotools.graph.build.basic org.geotools.graph.build.feature org.geotools.graph.build.line org.geotools.graph.build.opt org.geotools.graph.build.polygon org.geotools.graph.io org.geotools.graph.io.standard org.geotools.graph.path org.geotools.graph.structure org.geotools.graph.structure.basic org.geotools.graph.structure.line org.geotools.graph.structure.opt org.geotools.graph.traverse org.geotools.graph.traverse.basic org.geotools.graph.traverse.standard org.geotools.graph.util org.geotools.graph.util.delaunay org.geotools.graph.util.geom org.geotools.graph.util.graph org.geotools.gui.headless org.geotools.gui.swing org.geotools.gui.swing.contexttree org.geotools.gui.swing.contexttree.column org.geotools.gui.swing.contexttree.node org.geotools.gui.swing.contexttree.popup org.geotools.gui.swing.contexttree.renderer org.geotools.gui.swing.crschooser org.geotools.gui.swing.datachooser org.geotools.gui.swing.datachooser.model org.geotools.gui.swing.demo org.geotools.gui.swing.event org.geotools.gui.swing.filter org.geotools.gui.swing.icon org.geotools.gui.swing.image org.geotools.gui.swing.map.map2d org.geotools.gui.swing.map.map2d.control org.geotools.gui.swing.map.map2d.decoration org.geotools.gui.swing.map.map2d.event org.geotools.gui.swing.map.map2d.handler org.geotools.gui.swing.map.map2d.listener org.geotools.gui.swing.map.map2d.strategy org.geotools.gui.swing.misc org.geotools.gui.swing.misc.filter org.geotools.gui.swing.misc.Render org.geotools.gui.swing.process org.geotools.gui.swing.propertyedit org.geotools.gui.swing.propertyedit.filterproperty org.geotools.gui.swing.propertyedit.model org.geotools.gui.swing.propertyedit.styleproperty org.geotools.gui.swing.referencing org.geotools.gui.swing.style org.geotools.gui.swing.style.sld org.geotools.gui.swing.table org.geotools.gui.swing.tree org.geotools.image org.geotools.image.io org.geotools.image.io.metadata org.geotools.image.io.mosaic org.geotools.image.io.netcdf org.geotools.image.io.stream org.geotools.image.io.text org.geotools.image.jai org.geotools.image.palette org.geotools.index org.geotools.index.quadtree org.geotools.index.quadtree.fs org.geotools.index.rtree org.geotools.index.rtree.cachefs org.geotools.index.rtree.database org.geotools.index.rtree.database.mysql org.geotools.index.rtree.fs org.geotools.index.rtree.memory org.geotools.io org.geotools.jdbc org.geotools.kml org.geotools.kml.bindings org.geotools.legend org.geotools.map org.geotools.map.event org.geotools.math org.geotools.measure org.geotools.metadata org.geotools.metadata.iso org.geotools.metadata.iso.citation org.geotools.metadata.iso.constraint org.geotools.metadata.iso.content org.geotools.metadata.iso.distribution org.geotools.metadata.iso.extent org.geotools.metadata.iso.identification org.geotools.metadata.iso.lineage org.geotools.metadata.iso.maintenance org.geotools.metadata.iso.quality org.geotools.metadata.iso.spatial org.geotools.metadata.sql org.geotools.nature org.geotools.openoffice org.geotools.ows org.geotools.ows.bindings org.geotools.ows.v1_1 org.geotools.parameter org.geotools.process org.geotools.process.impl org.geotools.process.literal org.geotools.referencing org.geotools.referencing.crs org.geotools.referencing.cs org.geotools.referencing.datum org.geotools.referencing.example org.geotools.referencing.factory org.geotools.referencing.factory.epsg org.geotools.referencing.factory.wms org.geotools.referencing.operation org.geotools.referencing.operation.builder org.geotools.referencing.operation.matrix org.geotools.referencing.operation.projection org.geotools.referencing.operation.transform org.geotools.referencing.piecewise org.geotools.referencing.wkt org.geotools.renderer org.geotools.renderer.i18n org.geotools.renderer.lite org.geotools.renderer.lite.gridcoverage2d org.geotools.renderer.shape org.geotools.renderer.shape.shapehandler.jts org.geotools.renderer.shape.shapehandler.simple org.geotools.renderer.style org.geotools.repository org.geotools.repository.adaptable org.geotools.repository.defaults org.geotools.repository.postgis org.geotools.repository.property org.geotools.repository.shapefile org.geotools.repository.styling org.geotools.repository.wfs org.geotools.repository.wms org.geotools.sld org.geotools.sld.bindings org.geotools.styling org.geotools.styling.visitor org.geotools.svg org.geotools.test org.geotools.text org.geotools.text.filter org.geotools.util org.geotools.util.logging org.geotools.utils org.geotools.utils.coveragetiler org.geotools.utils.imagemosaic org.geotools.utils.imageoverviews org.geotools.utils.imagepyramid org.geotools.utils.progress org.geotools.validation org.geotools.validation.attributes org.geotools.validation.dto org.geotools.validation.network org.geotools.validation.relate org.geotools.validation.spatial org.geotools.validation.xml org.geotools.wfs org.geotools.wfs.bindings org.geotools.wfs.protocol org.geotools.wfs.v_1_0_0.data org.geotools.wfs.v_1_1_0.data org.geotools.xlink org.geotools.xml org.geotools.xml.filter org.geotools.xml.gml org.geotools.xml.handlers org.geotools.xml.handlers.xsi org.geotools.xml.impl org.geotools.xml.impl.jxpath org.geotools.xml.schema org.geotools.xml.schema.impl org.geotools.xml.styling org.geotools.xml.test org.geotools.xml.transform org.geotools.xml.wfs org.geotools.xml.xLink org.geotools.xml.xsi org.geotools.xs org.geotools.xs.bindings org.geotools.xs.facets
<think&gt;好的,我现在需要帮助用户解决使用Java生成河南气象图的问题。用户的需求是根据经纬度坐标和对应的值,通过插值生成格点数据,然后使用河南的Shapefile图层绘制气象图,不同值用不同颜色填充,无数据的地方保持原色,并保存图片。用户还希望得到完整的代码示例。首先,我需要回顾之前的对话。用户之前已经询问过如何使用Java进行IDW插值并绘制河南地图,现在更具体地要求结合Shapefile文件,并指定颜色映射。用户提供的引用中提到了Shapefile的结构、GeoJSON处理以及多边形判断的方法,这些都可能对解决问题有帮助。 接下来,我需要确定用户的需求细节:1.使用河南的Shapefile(.shp)作为底图。2.根据经纬度坐标和值进行插值(可能还是IDW)。3.插值后的格点数据根据值着色:1蓝色,2黄色,3红色。 4.无数据的区域保持Shapefile的原色。 5.生成完整Java代码。首先,处理Shapefile需要使用GeoTools库。需要添加相关依赖,如gt-shapefile、gt-swing等。然后读取河南的Shapefile,获取其几何边界,用于后续的插值范围和绘图。 插值部分,用户之前已经实现了IDW算法,这里可能需要调整参数或集成到当前代码中。需要生成覆盖河南区域的格点,对每个格点进行插值计算,得到对应的值。颜色映射方面,需要根据插值结果的值(1、2、3)分配颜色。但实际插值结果可能是连续值,可能需要将值分段映射到这三个颜色。或者用户可能希望将离散值1、2、3对应不同颜色,这时候可能需要分类插值结果,或者用户的数据本来就是离散的分类值。需要确认这一点,但用户的问题描述中提到“不同值对应不同颜色”,可能是指离散值,所以需要将插值结果四舍五入或分类到这三个值,或者可能用户的数据只有这三个值。需要假设用户的数据是离散的,或者需要将连续值离散化到这三个类别。 绘图部分,使用GeoTools的样式功能,创建栅格图层,根据值设置颜色,同时保留Shapefile的原色填充。可能需要将插值结果生成一个栅格图像,然后叠加在Shapefile的矢量图层上。可能的步骤: 1.读取河南的Shapefile,获取其边界范围,确定插值的格点范围。 2.使用IDW插值算法生成格点数据。3.创建栅格图层,根据值设置颜色(1-蓝,2-黄,3-红)。4.将栅格图层和Shapefile矢量图层叠加,确保无数据区域显示原色。 5.保存为图片。需要注意的问题: -Shapefile的坐标系可能需要转换,尤其是如果经纬度是WGS84,而Shapefile可能使用其他投影,需要确保坐标系统一。-插值格点的分辨率需要合适,太密会影响性能,太疏会影响图像质量。-颜色映射需要正确应用到栅格数据,可能需要使用GeoTools的RasterSymbolizer。-确保矢量图层在原色填充时不被覆盖,可能需要调整图层顺序或样式。现在需要编写代码,整合这些步骤。首先添加GeoTools依赖,然后读取Shapefile,生成插值格点,创建样式,渲染地图并保存。可能的代码结构:-读取Shapefile并获取几何边界。 -生成格点数据,使用IDW插值。-将格点数据转换为GeoTools支持的GridCoverage。-创建带有颜色映射的样式。 -创建地图内容,添加栅格和矢量图层。 -渲染并保存图像。 需要处理坐标转换,确保插值的经纬度与Shapefile的几何一致。可能需要使用GeoTools的CoordinateReferenceSystem处理。 此外,用户希望无数据区域保持原色,即Shapefile的填充色不被覆盖。因此,可能需要将矢量图层置于顶层,或者设置栅层的透明度,使得没有数据的区域显示底图颜色。或者,在插值时,只对河南区域内的点进行插值,区域外的保持透明,但需要确保栅格覆盖整个河南区域。可能遇到的挑战是如何将插值后的格点数据正确转换为GeoTools的GridCoverage2D对象,并应用样式。可能需要使用GridCoverageFactory,并定义Envelope(范围)和CoordinateReferenceSystem。 颜色映射部分,需要使用FeatureTypeStyle和RasterSymbolizer,定义ContrastEnhancement和ColorMap,将值映射到指定颜色。需要验证代码是否正确处理了Shapefile的读取,插值是否正确生成,颜色是否正确应用,以及输出图片是否包含河南的边界和正确颜色。最后,生成相关问题,可能涉及性能优化、其他插值方法、颜色映射调整等。</think&gt;### Java实现河南气象图生成的完整解决方案 #### 一、环境准备 ```xml <!-- pom.xml依赖 --&gt; <dependencies&gt; <!-- GeoTools核心库 --&gt; <dependency&gt; <groupId&gt;org.geotools</groupId&gt; <artifactId&gt;gt-shapefile</artifactId&gt; <version&gt;28.2</version&gt; </dependency&gt; <dependency&gt; <groupId&gt;org.geotools</groupId&gt; <artifactId&gt;gt-coverage</artifactId&gt; <version&gt;28.2</version&gt; </dependency&gt; <dependency&gt; <groupId&gt;org.geotools</groupId&gt; <artifactId&gt;gt-swing</artifactId&gt; <version&gt;28.2</version&gt; </div&gt; </dependencies&gt; ``` #### 二、核心实现代码 ```java import org.geotools.coverage.grid.*; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.factory.CommonFactoryFinder; import org.geotools.gce.geotiff.GeoTiffWriter; import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.map.FeatureLayer; import org.geotools.map.MapContent; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.geotools.styling.*; import org.opengis.filter.FilterFactory; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; public class HenanWeatherMap { // 颜色映射规则 private static final Color[] COLORS = { new Color(0, 0, 255), // 蓝色对应值1 new Color(255, 255, 0), // 黄色对应值2 new Color(255, 0, 0) // 红色对应值3 }; public static void main(String[] args) throws Exception { // 1. 读取河南边界Shapefile File shapeFile = new File("henan.shp"); FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile); SimpleFeatureSource featureSource = store.getFeatureSource(); // 2. 生成插值格网数据(假设已实现IDW插值) GridCoverage2D coverage = generateGridCoverage(featureSource); // 3. 创建地图样式 Style rasterStyle = createRasterStyle(); Style vectorStyle = SLD.createPolygonStyle(Color.LIGHT_GRAY, null, 1); // 4. 构建地图内容 MapContent mapContent = new MapContent(); mapContent.addLayer(new GridCoverageLayer(coverage, rasterStyle)); mapContent.addLayer(new FeatureLayer(featureSource, vectorStyle)); // 5. 渲染并保存图像 BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); mapContent.render(g2d); g2d.dispose(); ImageIO.write(image, "png", new File("henan_weather.png")); } // 生成栅格数据(示例) private static GridCoverage2D generateGridCoverage(SimpleFeatureSource featureSource) { // 获取河南边界范围 ReferencedEnvelope env = featureSource.getBounds(); // 创建格网参数(0.01度分辨率) GridGeometry2D gridGeometry = new GridGeometry2D( new GridEnvelope2D(0, 0, 800, 600), env ); // 生成示例数据(需替换为实际插值逻辑) float[][] data = new float[600][800]; for (int y=0; y<600; y++) { for (int x=0; x<800; x++) { data[y][x] = (x % 3) + 1; // 示例数据 } } return GridCoverageFactory.create( "weather", data, env ); } // 创建栅格样式 private static Style createRasterStyle() { FilterFactory ff = CommonFactoryFinder.getFilterFactory(); StyleBuilder sb = new StyleBuilder(); // 定义颜色映射规则 List<ColorMapEntry&gt; entries = new ArrayList<&gt;(); entries.add(sb.createColorMapEntry( ff.literal(1), sb.colorExpression(COLORS[0]), null, null, null )); entries.add(sb.createColorMapEntry( ff.literal(2), sb.colorExpression(COLORS[1]), null, null, null )); entries.add(sb.createColorMapEntry( ff.literal(3), sb.colorExpression(COLORS[2]), null, null, null )); RasterSymbolizer symbolizer = sb.createRasterSymbolizer(); ColorMap colorMap = sb.createColorMap(); colorMap.setColorMapEntries(entries.toArray(new ColorMapEntry[0])); symbolizer.setColorMap(colorMap); return SLD.wrapSymbolizers(symbolizer); } } ``` #### 三、关键实现说明 1. **坐标系统处理** 使用`DefaultGeographicCRS.WGS84`确保经纬度坐标与Shapefile坐标系一致[^1] 2. **颜色映射原理** 通过`ColorMap`实现离散值到颜色的映射,使用`RasterSymbolizer`控制栅格渲染方式 3. **图层叠加顺序** 先添加栅格层再添加矢量层,确保边界线始终可见[^2] 4. **插值数据集成** `generateGridCoverage`方法需要替换为实际插值算法实现(参考IDW算法) #### 四、效果示意图 ```plaintext 生成图像包含: - 河南省矢量边界(浅灰色填充) - 气象数据颜色分布: █ 蓝色区域(值=1) █ 黄色区域(值=2) █ 红色区域(值=3) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弗里曼的小伙伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值