GeoTools:在地图上绘制多边形

时间:2013-11-25 15:13:07

标签: java gps gis geotools

我是Java的GeoTools库的新手,我只是想在地图上绘制一个多边形。我正在使用GPS坐标来绘制得很好,但是我无法弄清楚如何在它们之间绘制LineString以挽救我的生命。

我已经检查了geotools.org上的所有教程以及this posting,但没有用。 这应该这么复杂吗?任何人都可以发布绘制LineString所需的代码片段吗?这是我上次尝试过的:

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

谢谢,提前和最好的问候

2 个答案:

答案 0 :(得分:0)

您似乎在混合几何类型,尝试类似:

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

为我提供了正确的答案。

答案 1 :(得分:0)

一直在与此作斗争。最后,它使用来自网络的不同片段的混合来将地图保存到图像(png)。通过JMapFrame.showMap(map);显示地图会导致异常和崩溃。无论如何,我需要图像。具有两个点的线的示例如下所示。添加具有更多点的折线应该相同:

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