4

I have a shapefile that is opened and looks as such:

Java Geotools Demo

Now I am attempting to draw a line from two points I click on that map; however the code they give for the QuickStart.java example is exceptionally 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, where I am confused is as to what method I should use to click two points and draw a line between them?

Anyone have any good resources on this? I've read up on the GeoTools documentation and am still a bit confused.

I attempted to do the generic 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);
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64
  • For more information; I am just following along with the various Geotool tutorials; I attempted their "Style" tutorial and that left me without any luck as well. – Timothy Frisch Jul 01 '14 at 20:24
  • Do you also have a code, where you append the line to layer, and show that layer? Your snippets don't contain those lines for my eyes. Generating a line object in memory does not mean anything would come to screen. – mico Jul 07 '14 at 19:03
  • I had done a little bit of work with the geometric factory but I am not sure how layers work out in geotools. I had spent quite a few days working with it with no fruition. – Timothy Frisch Jul 07 '14 at 19:16

1 Answers1

3

Adding the line to a collection which you can add to the layer:

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

For the creation of the SimpleFeature, see also the Feature Tutorial

Jur_
  • 302
  • 5
  • 14
  • The tutorial you pointed just show HOW to draw Features. You went further and show WHY we have to use each class. I've a little lost until now on creating features on map just copying/pasting snipets without really know why. Thanks. – Magno C Mar 30 '23 at 16:17