随机地理坐标(在陆地上,避开海洋) [英] Random geographic coordinates (on land, avoid ocean)

查看:182
本文介绍了随机地理坐标(在陆地上,避开海洋)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何生成地球上地点的随机坐标(纬度/经度)的任何聪明的想法?纬度/经度.精确到5分,避免水体.

Any clever ideas on how to generate random coordinates (latitude / longitude) of places on Earth? Latitude / Longitude. Precision to 5 points and avoid bodies of water.

    double minLat = -90.00;
    double maxLat = 90.00;      
    double latitude = minLat + (double)(Math.random() * ((maxLat - minLat) + 1));
    double minLon = 0.00;
    double maxLon = 180.00;     
    double longitude = minLon + (double)(Math.random() * ((maxLon - minLon) + 1));
    DecimalFormat df = new DecimalFormat("#.#####");        
    log.info("latitude:longitude --> " + df.format(latitude) + "," + df.format(longitude));

也许我生活在一个梦幻世界中,水的话题是不可避免的...但是希望有一种更好,更清洁,更有效的方法来实现这一目标?

Maybe i'm living in a dream world and the water topic is unavoidable ... but hopefully there's a nicer, cleaner and more efficient way to do this?

编辑

一些奇妙的答案/想法-但是,从规模上来说,我需要生成25,000个坐标.由于延迟,成本和其他一些因素,去外部服务提供商可能不是最好的选择.

Some fantastic answers/ideas -- however, at scale, let's say I need to generate 25,000 coordinates. Going to an external service provider may not be the best option due to latency, cost and a few other factors.

推荐答案

要解决水问题,很大程度上将是一个数据问题,例如您是想错过海洋还是要错过小溪流?您需要使用具有所需数据质量的服务,或者需要自己获取数据并在本地运行.从您的编辑看来,您似乎想走本地数据路线,所以我将重点介绍一种方法.

To deal with the body of water problem is going to be largely a data issue, e.g. do you just want to miss the oceans or do you need to also miss small streams. Either you need to use a service with the quality of data that you need, or, you need to obtain the data yourself and run it locally. From your edit, it sounds like you want to go the local data route, so I'll focus on a way to do that.

一种方法是获取陆地或水域的shapefile.然后,您可以生成一个随机点,并确定它是否与陆地区域相交(或者,不与水域相交).

One method is to obtain a shapefile for either land areas or water areas. You can then generate a random point and determine if it intersects a land area (or alternatively, does not intersect a water area).

要开始使用,您可能会在此处获得一些低分辨率的数据,然后在

To get started, you might get some low resolution data here and then get higher resolution data here for when you want to get better answers on coast lines or with lakes/rivers/etc. You mentioned that you want precision in your points to 5 decimal places, which is a little over 1m. Do be aware that if you get data to match that precision, you will have one giant data set. And, if you want really good data, be prepared to pay for it.

一旦有了形状数据,就需要一些工具来帮助您确定随机点的交点. Geotools 是一个很好的起点,可能会满足您的需求.您还将最终查看opengis代码(geotools网站下的文档-不确定它们是否消耗了它们或什么)和 JTS 用于几何处理.使用此工具,您可以快速打开shapefile并开始执行一些交集查询.

Once you have your shape data, you need some tools to help you determine the intersection of your random points. Geotools is a great place to start and probably will work for your needs. You will also end up looking at opengis code (docs under geotools site - not sure if they consumed them or what) and JTS for the geometry handling. Using this you can quickly open the shapefile and start doing some intersection queries.

    File f = new File ( "world.shp" );
    ShapefileDataStore dataStore = new ShapefileDataStore ( f.toURI ().toURL () );
    FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = 
        dataStore.getFeatureSource ();
    String geomAttrName = featureSource.getSchema ()
        .getGeometryDescriptor ().getLocalName ();

    ResourceInfo resourceInfo = featureSource.getInfo ();
    CoordinateReferenceSystem crs = resourceInfo.getCRS ();
    Hints hints = GeoTools.getDefaultHints ();
    hints.put ( Hints.JTS_SRID, 4326 );
    hints.put ( Hints.CRS, crs );

    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2 ( hints );
    GeometryFactory gf = JTSFactoryFinder.getGeometryFactory ( hints );

    Coordinate land = new Coordinate ( -122.0087, 47.54650 );
    Point pointLand = gf.createPoint ( land );
    Coordinate water = new Coordinate ( 0, 0 );
    Point pointWater = gf.createPoint ( water );

    Intersects filter = ff.intersects ( ff.property ( geomAttrName ), 
        ff.literal ( pointLand ) );
    FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource
            .getFeatures ( filter );

    filter = ff.intersects ( ff.property ( geomAttrName ), 
        ff.literal ( pointWater ) );
    features = featureSource.getFeatures ( filter );

快速说明:

  1. 这假定您获得的shapefile是多边形数据.线或点的相交不会给您想要的东西.
  2. 第一部分打开shapefile-没什么有趣的
  3. 您必须获取给定文件的几何属性名称
  4. 坐标系的内容-您在帖子中指定了纬度/经度,但是GIS可能要复杂得多.通常,我指的是 geographic,wgs84 ,这就是我在此处设置的数据.但是,如果不是这种情况,那么您需要确保以正确的坐标系处理数据.如果这一切听起来像胡言乱语,请在Google周围搜索有关GIS/坐标系/基准/椭球的教程.
  5. 生成坐标几何,并且过滤器非常不言自明.生成的要素集将为空,即如果您的数据为土地覆盖,则坐标在水中;或者为空,即相反.
  1. This assumes the shapefile you got is polygon data. Intersection on lines or points isn't going to give you what you want.
  2. First section opens the shapefile - nothing interesting
  3. you have to fetch the geometry property name for the given file
  4. coordinate system stuff - you specified lat/long in your post but GIS can be quite a bit more complicated. In general, the data I pointed you at is geographic, wgs84, and, that is what I setup here. However, if this is not the case for you then you need to be sure you are dealing with your data in the correct coordinate system. If that all sounds like gibberish, google around for a tutorial on GIS/coordinate systems/datum/ellipsoid.
  5. generating the coordinate geometries and the filters are pretty self-explanatory. The resulting set of features will either be empty, meaning the coordinate is in the water if your data is land cover, or not empty, meaning the opposite.

注意:如果您使用一组非常随机的点来进行此操作,则您经常会碰到水,可能要花一些时间才能达到25k点.您可能想尝试将点生成的作用范围比真正随机的要好(例如删除大西洋/太平洋/印度洋的大块).

Note: if you do this with a really random set of points, you are going to hit water pretty often and it could take you a while to get to 25k points. You may want to try to scope your point generation better than truly random (like remove big chunks of the Atlantic/Pacific/Indian oceans).

此外,您可能会发现相交查询过慢.如果是这样,您可能要考虑使用 GDAL 之类的工具来创建四叉树索引(qix).不过,我不记得geotools支持哪些索引类型.

Also, you may find that your intersection queries are too slow. If so, you may want to look into creating a quadtree index (qix) with a tool like GDAL. I don't recall which index types are supported by geotools, though.

这篇关于随机地理坐标(在陆地上,避开海洋)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆