我有一个打开的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);
最佳答案 将行添加到可以添加到图层的集合中:
>您应该从LineString创建一个SimpleFeature(这样几何线实际上得到GeoReferenced).确保添加逼真的坐标:第二个剪切的坐标将产生一个相对较小的线.
>创建一个新的DefaultFeatureCollection(存储器存储的集合),您可以添加SimpleFeature. (现在,在第二次剪切时,该线未添加到任何地方进行绘制.)
DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
lineCollection.add(simpleFeature);
>将DefaultFeatureCollection而不是FeatureSource添加到新创建的图层作为构造函数参数.
Layer layer = new FeatureLayer(lineCollection, style);
>将图层添加到地图中,就像在第一个剪切中一样.
map.addLayer(layer);
有关SimpleFeature的创建,另请参见Feature Tutorial