我有一个打开的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);
答案 0 :(得分:2)
将行添加到可添加到图层的集合中:
创建一个新的DefaultFeatureCollection(内存存储集合),您可以添加SimpleFeature。 (现在,在第二个剪断的线上没有添加任何地方用于绘图。)
DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
lineCollection.add(simpleFeature);
将DefaultFeatureCollection而不是FeatureSource添加到新创建的图层作为构造函数参数。
Layer layer = new FeatureLayer(lineCollection, style);
将图层添加到地图中,就像第一次剪切一样。
map.addLayer(layer);
要创建SimpleFeature,另请参阅Feature Tutorial