gpt4 book ai didi

java - 在 map 上显示连接点的线 - 接收空 map

转载 作者:行者123 更新时间:2023-11-29 08:34:34 26 4
gpt4 key购买 nike

我想绘制一条连接 map 上两个点的线。

我使用的代码:

public class Quickstart {


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

GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();

// ask for current and destination positions
double latitude, longitude, latitudeDest, longitudeDest;
Scanner reader = new Scanner(System.in);
reader.useLocale(Locale.US);
System.out.println("Enter reference longitude and latitude:\n");
longitude = reader.nextDouble();
latitude = reader.nextDouble();
System.out.println("Enter destination longitude and latitude:\n");
longitudeDest = reader.nextDouble();
latitudeDest = reader.nextDouble();
reader.close();

final String EPSG4326 = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\"," +
"\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\", " +
"0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";

CoordinateReferenceSystem crs = CRS.parseWKT(EPSG4326);


Point start = gf.createPoint(new Coordinate(longitude, latitude));
Point end = gf.createPoint(new Coordinate(longitudeDest, latitudeDest));

GeodeticCalculator gc = new GeodeticCalculator(crs);
gc.setStartingPosition(JTS.toDirectPosition(start.getCoordinate(), crs));
gc.setDestinationPosition(JTS.toDirectPosition(end.getCoordinate(), crs));

// Calculate distance between points
double distance = gc.getOrthodromicDistance();

int totalmeters = (int) distance;
int km = totalmeters / 1000;
int meters = totalmeters - (km * 1000);
float remaining_cm = (float) (distance - totalmeters) * 10000;
remaining_cm = Math.round(remaining_cm);
float cm = remaining_cm / 100;

System.out.println("Distance = " + km + "km " + meters + "m " + cm + "cm");

SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("TwoDistancesType");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add("location", Point.class);

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

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
featureBuilder.add(start);
//featureBuilder.add(end);

SimpleFeature feature = featureBuilder.buildFeature(null);
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
featureCollection.add(feature);

Style style = SLD.createSimpleStyle(TYPE, Color.red);
Layer layer = new FeatureLayer(featureCollection, style);

// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("TEST");

map.addLayer(layer);

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


}

}

我有两个问题:

1) 我无法向 featureBuilder 添加第二个功能。它不允许这样做。它显示 Can handle 1 attributes only, index is 1

那么,我该如何画一条线呢?

2) 通过上面的代码,我收到了:

org.geotools.renderer.lite.StreamingRenderer fireErrorEvent SEVERE: The scale denominator must be positive
java.lang.IllegalArgumentException: The scale denominator must be positive

-------- 更新------------------------

在@Michael 给出第一个问题的解决方案之后,现在我不再收到关于分母的错误,但我收到一张空 map (空白)。

----- 根据@iant 建议更新----------------

所以,我尝试了这个。创建了一个包含点(开始和结束)坐标的坐标,然后使用这些坐标创建了一个线串并将其添加到 featurebuilder。

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("TwoDistancesType");
builder.setCRS(DefaultGeographicCRS.WGS84);

builder.add("line", LineString.class); //added a linestring class


final SimpleFeatureType TYPE = builder.buildFeatureType();

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

Coordinate[] coordinates = {start.getCoordinate(), end.getCoordinate()};
LineString line = gf.createLineString(coordinates);
featureBuilder.add(line);

即使我正在加载 map (countries.shp),它也会显示一个带红线的空白区域。

------ 解决方案 --------------

好的,所以解决方案是(感谢@iants 评论):

样式 style = SLD.createLineStyle(Color.red, 2.0f); 图层layer = new FeatureLayer(featureCollection, style);

// Create style for the file
Style shpStyle = SLD.createSimpleStyle(TYPE, Color.blue);
Layer shpLayer = new FeatureLayer(featureSource, shpStyle);

// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("TEST");
map.addLayer(layer);
map.addLayer(shpLayer);

现在您在蓝色 map 上看到了一条红线!

最佳答案

您需要根据您的点创建一个 LineString,然后将其存储在您的要素中。

然后您应该获得正确的比例,但您可能希望先向 map 添加一些其他数据,例如海岸线。快速入门教程可以向您展示如何做到这一点。

关于java - 在 map 上显示连接点的线 - 接收空 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45214550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com