如何在java中只生成海洋坐标并在可视化Map中显示它们

cwdobuhd  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(324)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

25天前关门了。
改进这个问题
我正在做一个跟踪应用程序,它需要为不同的船只生成坐标,并在Map上显示它们的位置。
我研究过类似的问题,但都是关于陆地坐标的。另外,我也看到过很多不同的框架,比如geotools或unfoling,但是文档太大了,我找不到我要找的东西。
我只需要在海洋上生成坐标,然后在Map上显示标签。
在python中,有一个库可以检查一个点是否在陆地上(因此可以检查它是否在海洋上),然后使用folium可以添加带有传统信息的标记。
不知道java是否有类似的方法。

oxf4rvwz

oxf4rvwz1#

我会这样做:
下载一组陆地多边形
将它们存储在空间索引中
生成一个随机点并测试它是否落在地上
继续发电直到它不落在陆地上
重复,直到你有足够的分数

public class PointInSea {
 private static final FilterFactory2 FF = CommonFactoryFinder.getFilterFactory2();
 private SimpleFeatureCollection features;

 public static void main(String[] args) throws IOException {
   URL url = new URL("https://datahub.io/core/geo-countries/r/countries.geojson");
   Map<String, Object> params = new HashMap<>();
   params.put(GeoJSONDataStoreFactory.URL_PARAM.key, url);
   DataStore ds = DataStoreFinder.getDataStore(params);
   PointInSea me = new PointInSea();
   me.setFeatures(ds.getFeatureSource(ds.getTypeNames()[0]).getFeatures());
   List<Point> ret = me.getPoints(10);
   for (Point p : ret) {
     System.out.println(p);
   }
 }

 private void setFeatures(SimpleFeatureCollection features2) throws IOException {
   features = new SpatialIndexFeatureCollection(features2);
 }

 private List<Point> getPoints(int i) {
   List<Point> ret = new ArrayList<>(i);
   for (int count = 0; count < i; count++) {
     Point p = createRandomPoint();

     Contains filter = FF.contains(FF.property(features.getSchema().getGeometryDescriptor().getLocalName()), 
         FF.literal(p));
     while (features.subCollection(filter).size() != 0) {
       p = createRandomPoint();
       filter = FF.contains(FF.property(features.getSchema().getGeometryDescriptor().getLocalName()), FF.literal(p));
     }
     ret.add(p);
   }
   return ret;
 }

 public static Point createRandomPoint() {
   double latitude = (Math.random() * 180.0) - 90.0;
   double longitude = (Math.random() * 360.0) - 180.0;
   GeometryFactory geometryFactory = new GeometryFactory();
   Point point;
   if (CRS.getAxisOrder(DefaultGeographicCRS.WGS84) == CRS.AxisOrder.EAST_NORTH) {
     /* Longitude (= x coord) first ! */
     point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
   } else {
     point = geometryFactory.createPoint(new Coordinate(latitude, longitude));
   }
   return point;
 }

}
pieyvz9o

pieyvz9o2#

下载我的海洋网格坐标:
https://djinesse.com/public/coordinates_ocean.json 该文件可能在不久的将来被删除。
如何使用:

String fromFile = FileUtils.readFileToString( new File( "c:/users/you/downloads/coordinates.json" ), "UTF-8" );
JSONArray coordinates = new JSONArray( fromFile );
// pull random coordinates from the array:
int index = (int) (Math.random() * coordinates.length() );
JSONObject randomCoordinate = coordinates.getJSONObject( index );
double longitude = randomCoordinate.getDouble( "lo" );
double latitude = randomCoordinate.getDouble( "la" );

必修的 pom.xml 依赖项:

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency> 
</dependencies>

相关问题