【发布时间】:2014-06-29 00:12:53
【问题描述】:
我有一个打开的 shapefile,看起来像这样:
现在我正在尝试从我在该地图上单击的两个点画一条线;但是他们为QuickStart.java 示例提供的代码异常模糊。
这是他们的代码:
package org.geotools.tutorial;
import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.geometry.jts.JTSFactoryFinder;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;
/**
* Prompts the user for a shapefile and displays the contents on the screen in a map frame.
* <p>
* This is the GeoTools Quickstart application used in documentationa and tutorials. *
*/
public class Quickstart {
/**
* 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 {
// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
}
}
现在,我很困惑的是我应该使用什么方法来单击两个点并在它们之间画一条线?
谁有这方面的好资源?我已经阅读了 GeoTools 文档,但仍然有些困惑。
我尝试在网站上编写通用代码,但它没有出现在地图上。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
Coordinate[] coords =
new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };
LineString line = geometryFactory.createLineString(coords);
Style style2 = SLD.createLineStyle(Color.BLUE, 1);
Layer layer2 = new FeatureLayer(featureSource, style2);
【问题讨论】:
-
更多信息;我只是跟随各种 Geotool 教程;我尝试了他们的“风格”教程,这也让我没有任何运气。
-
您是否也有代码,在其中将行附加到图层并显示该图层?你的 sn-ps 不包含我眼中的那些线条。在内存中生成一个线对象并不意味着任何东西都会出现在屏幕上。
-
我对几何工厂做了一些工作,但我不确定图层在 geotools 中是如何工作的。我花了好几天的时间来处理它,但没有任何结果。
标签: java gis shapefile geotools