maps - 在地图上放置标记 - GeoTools 25-SNAPSHOT - Java 11
问题描述
我在我的应用程序中使用 geotools 25-SNAPSHOT。我在我的应用程序(光栅)中显示了一张地图。现在我想在地图上的特定纬度、经度位置放置一个标记(图像或简单的彩色点)而不刷新。
在下面的代码中,标记是在鼠标单击时放置的,但在addLayer()
地图上会重新加载,这是不合适的。
import java.awt.Color;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.coverage.grid.io.GridFormatFinder;
import org.geotools.data.DataUtilities;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.GridCoverageLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
public class Test {
/**
* GeoTools Quickstart demo application. Prompts the user for a shapefile and
* displays its contents on the screen in a map frame
*/
public static void main(String[] args) throws Exception {
try {
JMapFrame mapFrame;
File file = new File("mapfile.tif");
GridFormatFinder.scanForPlugins();
AbstractGridFormat format = GridFormatFinder.findFormat(file);
GridCoverage2DReader reader = format.getReader(file);
GridCoverage2D coverage = (GridCoverage2D) reader.read(null);
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Marker test");
StyleBuilder sb = new StyleBuilder();
Style baseStyle = sb.createStyle(sb.createRasterSymbolizer());
map.addLayer(new GridCoverageLayer(coverage, baseStyle));
mapFrame = new JMapFrame(map);
mapFrame.enableToolBar(true);
mapFrame.enableStatusBar(true);
//ToolBar
JToolBar toolBar = mapFrame.getToolBar();
Icon icon = new ImageIcon("icon.png");
JButton btn = new JButton(icon);
btn.setToolTipText("Add mining manually");
toolBar.addSeparator();
toolBar.add(btn);
//Action on mouse click for adding marker
btn.addActionListener(e -> mapFrame.getMapPane().setCursorTool(new CursorTool() {
@Override
public void onMouseClicked(MapMouseEvent ev) {
try {
DirectPosition2D p = ev.getWorldPos();
System.out.println(p.getX() + " -- " + p.getY());
Layer layer = getLayerPointByCoord(new Coordinate(p.getX(), p.getY()));
map.addLayer(layer);
} catch (Exception e) {
e.printStackTrace();
}
}
}));
mapFrame.setTitle("Aeron GIS Utility");
mapFrame.setSize(2030,830);
mapFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
static Layer getLayerPointByCoord(Coordinate coords) throws SchemaException {
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Point point = geometryFactory.createPoint(coords);
SimpleFeatureType TYPE = DataUtilities.createType("test", "point", "the_geom:Point");
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) TYPE);
featureBuilder.add(point);
SimpleFeature feature = featureBuilder.buildFeature("LineString_Sample");
DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
lineCollection.add(feature);
Style style = SLD.createPointStyle("Circle", Color.BLACK, Color.RED, 1f, 15f);
return new FeatureLayer(lineCollection, style);
}
}
解决方案
推荐阅读
- java - 无法为 SyncResponse Payload 创建设备对象
- angular - 如何在这个 Angular6 表单中自动插入日期时间?
- python - Python 性能问题 - 从特定 Pickle 文件中读取相同 Pandas 数据帧的多种方法
- excel - 日期(现在)显示 1900 年 1 月?
- sql - 确定 4 个连续的观察结果
- linux - Linux 文件默认权限
- excel - 在一个范围内输入的文本会导致另一个范围被删除
- karate - 如何将全局变量传递给可重用的要素类路径?
- c++ - 通过宏内联和包含源之间的区别
- python - Tensorflow Keras model.summary() 在层上显示 0 个可训练参数