How to draw a line on a shapefile using Geotools

I have a shapefile that opens and looks like this:

Java Geotools Demo

Now I'm trying to draw a line of two points, which I click on this map; however, the code they give as an example QuickStart.javais extremely vague.

Here is their code:

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);
    }

}

Now that I'm confused about which method I should use to click two dots and draw a line between them?

Does anyone have any good resources? I read the GeoTools documentation and am still a bit confused.

I tried to make a common code on the site, but it does not appear on the map.

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);
+4
source share
1 answer

, :

  • SimpleFeature LineString ( , GeoReferenced). , : .
  • DefaultFeatureCollection (, ), SimpleFeature. ( , .)

    DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
    lineCollection.add(simpleFeature);
    
  • DefaultFeatureCollection, FeatureSource .

    Layer layer = new FeatureLayer(lineCollection, style);
    
  • , .

    map.addLayer(layer);
    

SimpleFeature . Feature Tutorial

+2

Source: https://habr.com/ru/post/1546447/


All Articles