【问题标题】:GeoTools: Drawing polygons on mapGeoTools:在地图上绘制多边形
【发布时间】:2013-12-10 09:09:48
【问题描述】:

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

谢谢,提前和最好的问候

【问题讨论】:

  • 请添加一些代码,以便我们看到您尝试吃的东西。
  • 如果你检查行它包含什么?
  • 我放在那里的 10 个元素,带有一个合理的信封。但是 .showMap() 函数会抛出以下异常: org.geotools.geometry.iso.coordinate.LineStringImpl 不能转换为 com.vividsolutions.jts.geom.Geometry
  • 早点提一下错误可能是值得的 :-) 听起来您的构建器正在构建错误类型的 LineString,您使用的是 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  • 是的,我是。我之前没有提到这个错误,因为感觉好像我只是在黑暗中摸索,不知道自己在做什么。我尝试了其他也不起作用的变体,但显然出现了不同的错误。

标签: java gps gis geotools


【解决方案1】:

您似乎在混合几何类型,请尝试以下操作:

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

这给了我正确的答案。

【讨论】:

  • 啊好吧 - 我似乎误解了坐标系的概念。我想将坐标定义为纬度/经度。这可能吗?
  • 您可以将任何您喜欢的值放入坐标中(我只是选择了简单的整数)-
  • 这段代码只是实现了你的 LineString line = builder.createLineString(listOfPoints);
  • 好的,现在没有更多错误了,但不幸的是,地图上没有绘制任何内容:-/
【解决方案2】:

一直在与此作斗争。最后让它使用来自网络的不同sn-ps的混合将地图保存为图像(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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多