1

I am using the Geotools library to develop a Java Swing application. I am having a problem creating a layer with lines between points. Can someone help me?

Here my code:

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import java.awt.Color;
import static org.geotools.coverage.processing.operation.Interpolate.TYPE;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class DrawLine {
    public Layer getLayerLineByCoord(Coordinate[] coords) {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        LineString line = geometryFactory.createLineString(coords);

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) TYPE);
        featureBuilder.add(line);
        SimpleFeature feature = featureBuilder.buildFeature("LineString_Sample");

        DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
        lineCollection.add(feature);

        Style style = SLD.createLineStyle(Color.BLUE, 1);
        return new FeatureLayer(lineCollection, style);
    }
}
1
  • 1
    What is the problem, exactly? – John Powell Jul 21 '16 at 12:30
1

Your schema is wrong

import static org.geotools.coverage.processing.operation.Interpolate.TYPE;

should be

import org.opengis.feature.simple.SimpleFeatureType;

Which leads to you needing a line like:

SimpleFeatureType TYPE = DataUtilities.createType("test", "line", "the_geom:LineString"); 

to create your FeatureType (or schema).

Then it works fine.

import com.ianturton.cookbook.utilities.GenerateRandomData;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import java.awt.Color;

import org.geotools.data.DataUtilities;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
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.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class DrawLine {
    public Layer getLayerLineByCoord(Coordinate[] coords) throws SchemaException {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        LineString line = geometryFactory.createLineString(coords);
        SimpleFeatureType TYPE = DataUtilities.createType("test", "line", "the_geom:LineString");
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) TYPE);
        featureBuilder.add(line);
        SimpleFeature feature = featureBuilder.buildFeature("LineString_Sample");

        DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
        lineCollection.add(feature);

        Style style = SLD.createLineStyle(Color.BLUE, 1);
        return new FeatureLayer(lineCollection, style);
    }

    public static void main(String[] args) throws SchemaException {
      MapContent map = new MapContent();
      map.setTitle("Quickstart");

      DrawLine line = new DrawLine();
      LineString ls = GenerateRandomData.createRandomLineString(10);
      Layer layer = line.getLayerLineByCoord(ls.getCoordinates());
      map.addLayer(layer);


      // Now display the map
      JMapFrame.showMap(map);

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.