如何使用Geotools在shapefile上绘制线?

4

我有一个打开的shapefile,看起来是这样的:

Java Geotools Demo

现在我正在尝试从地图上我点击的两个点之间画一条线;然而他们给出的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教程;我正在跟随它们的“样式”教程,但是我也没有成功。 - Timothy Frisch
你是否也有一段代码,可以将行添加到图层并显示该图层?对于我来说,你的片段中没有包含这些行。在内存中生成一条线对象并不意味着会在屏幕上显示出来。 - mico
我曾经在几何工厂做过一些工作,但我不确定图层在geotools中是如何工作的。我花了很多天的时间去尝试,但没有取得任何成果。 - Timothy Frisch
1个回答

3

将该行添加到集合中,然后可以将其添加到图层中:

  • You should create a SimpleFeature from your LineString (this is such that the geometric line actually gets GeoReferenced). Make sure you add realistic coordinates though: The coordinates from the 2nd snipped would result in a relatively small line.
  • Create a new DefaultFeatureCollection (memory-stored collection) to which you can add the SimpleFeature. (Right now, in the 2nd snipped the line isn't added anywhere for drawing.)

    DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
    lineCollection.add(simpleFeature);
    
  • Add the DefaultFeatureCollection rather than the FeatureSource to the new created layer as a constructor parameter.

    Layer layer = new FeatureLayer(lineCollection, style);
    
  • add the layer to the map like in your first snipped.

    map.addLayer(layer);
    
有关SimpleFeature的创建,请参见要素教程

你指出的教程只展示了如何绘制要素。而你更进一步,展示了为什么我们需要使用每个类。在创建地图要素时,我之前只是复制/粘贴代码片段,没有真正了解为什么。非常感谢你的分享。 - Magno C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接