2

I'm new to the GeoTools library for Java and I'm simply trying to draw a polygon on a map. I'm using GPS coordinates for points, which draw just fine, but I just can't figure out how to draw the LineString between them to save my life.

I have checked all the tutorials on geotools.org and also this posting but no avail. Should this be so complicated? Can anybody maybe post the code fragments required to draw a LineString? This is what I've tried last:

SimpleFeatureType lineType = DataUtilities.createType("LINE", "geom:LineString,name:String");
SimpleFeatureBuilder featureBuilderLines = new SimpleFeatureBuilder(lineType);
SimpleFeatureCollection collectionLines = FeatureCollections.newCollection();

LineString line = builder.createLineString(listOfPoints);
featureBuilderLines.add(line);
SimpleFeature featureLine = featureBuilderLines.buildFeature(null);
((DefaultFeatureCollection)collectionLines).add(featureLine);     
Style lineStyle = SLD.createLineStyle(Color.RED, 2.0f);
map.addLayer(new FeatureLayer(collectionLines, lineStyle));

Thanks and advance and best regards

Community
  • 1
  • 1
user3032769
  • 21
  • 1
  • 4
  • Please add some code so we can see what you ate attempting. – Ian Turton Nov 26 '13 at 07:27
  • if you inspect line what does it contain? – Ian Turton Nov 26 '13 at 12:56
  • The 10 elements which I put in there, with a plausible envelope. However the .showMap() function throws the following exception: org.geotools.geometry.iso.coordinate.LineStringImpl cannot be cast to com.vividsolutions.jts.geom.Geometry – user3032769 Nov 26 '13 at 13:25
  • It might have been worth mentioning the error sooner :-) It sounds like your builder is building the wrong sort of LineString, are you using a GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); – Ian Turton Nov 26 '13 at 13:48
  • Yes I am. I didn't mention the error earlier because it feels as if I'm just poking around in the dark here, not knowing what I'm doing. I tried other variations which also didn't work but then with different errors obviously. – user3032769 Nov 26 '13 at 14:41

2 Answers2

1

You seem to be mixing Geometry types, try something like:

import org.geotools.geometry.jts.JTSFactoryFinder;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;

public class TestLineBuilder {
    public static void  main(String[] args) {

    com.vividsolutions.jts.geom.GeometryFactory gFac = JTSFactoryFinder.getGeometryFactory();
    Coordinate[] coordinates = new Coordinate[2];
    coordinates[0] = new Coordinate(1,3);
    coordinates[1] = new Coordinate(3,8);
    LineString line =gFac.createLineString(coordinates );
    System.out.println(line);
    }
}

which gives the right sort of answer for me.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
0

Have been fighting with this. Finally got it to work for saving the map to an image (png) using a mixture of different snippets from the web. Showing the map via JMapFrame.showMap(map); leads to an Exception and crash. Anyway, I needed the image. The example for a line with two points is shown below. Adding a polyline with more points should be identical:

SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

b.setName( "LineFeature" );

//add a geometry property
b.setCRS( DefaultGeographicCRS.WGS84 ); // set crs first
b.add( "line", LineString.class ); // then add geometry

//build the type
final SimpleFeatureType TYPE = b.buildFeatureType();

SimpleFeatureBuilder featureBuilderLines = new SimpleFeatureBuilder(TYPE);

SimpleFeatureCollection collectionLines = new DefaultFeatureCollection("internal",TYPE);

GeometryFactory gFac = JTSFactoryFinder.getGeometryFactory(JTSFactoryFinder.EMPTY_HINTS);
Coordinate[] coordinates = new Coordinate[2];

double latStart = 44.9;
double lonStart = 14.9;

double latEnd = 12.1;
double lonEnd = 9.4;

coordinates[0] = new Coordinate(lonStart, latStart);
coordinates[1] = new Coordinate(lonEnd, latEnd);

LineString line = gFac.createLineString(coordinates );

featureBuilderLines.add(line);
SimpleFeature featureLine = featureBuilderLines.buildFeature(null);
collectionLines.add(featureLine);   

float lineWidt = 2.0f;

Style lineStyle = SLD.createLineStyle(Color.red, lineWidth);

SimpleFeatureSource collectionFeatureSource = new CollectionFeatureSource(collectionLines);

map.addLayer(collectionFeatureSource, lineStyle);
gnotor
  • 1