【问题标题】:How to draw a line on a shapefile using Geotools如何使用 Geotools 在 shapefile 上画一条线
【发布时间】: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


【解决方案1】:

将线添加到可以添加到图层的集合中:

  • 你应该从你的 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

【讨论】:

    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    相关资源
    最近更新 更多