如何按国家/地区查询 PostGIS 中的数据?

How to query data from PostGIS by country?

我已经从 TM_WORLD_BORDERS_SIMPL-0.3.zip

下载了世界地图 shapefile

世界地图table的栏目如下:

gid integer NOT NULL DEFAULT nextval('table_world_gid_seq'::regclass),
fips character varying(2) COLLATE pg_catalog."default",
iso2 character varying(2) COLLATE pg_catalog."default",
iso3 character varying(3) COLLATE pg_catalog."default",
un smallint,
name character varying(50) COLLATE pg_catalog."default",
area integer,
pop2005 bigint,
region smallint,
subregion smallint,
lon double precision,
lat double precision,
geom geometry(MultiPolygon,4326)

现在我想按geom列查询我的数据,比如查询中国的数据

//function in controller
@GetMapping("china")
fun getInChina(): Result<List<WalkItem>> {
    val ce = countryRepo.findById(30).get()
    val wes = walkRepo.findInCountry(ce.geom!!)
    return Result.success(wes)
}
//function in repository
@Query("SELECT new tech.return0er.donkeygo.model.WalkItem(w) FROM ${WalkEntity.TABLE_NAME} w WHERE ST_Intersects(ST_PointOnSurface(:area), w.start_point) = true")
fun findInCountry(area: Geometry): List<WalkItem>

但总是return空数据列表。我有中国区的数据。

PS: 我有另一个查询通过获取地图的左上角位置和右下角位置。

@Query("SELECT new tech.return0er.donkeygo.model.WalkItem(w, u) FROM ${WalkEntity.TABLE_NAME} w, ${UserEntity.TABLE_NAME} u WHERE INTERSECTS(w.start_point, :area) = true" +
            " AND w.visibility=${WalkEntity.VISIBLE_TO_ALL} AND w.state=${WalkEntity.STATE_VISIBLE}" +
            " AND w.uid=u.uid" +
            " ORDER BY w.start_time DESC")
fun findWithin(area: Geometry, pageable: Pageable): List<WalkItem>
@GetMapping("within")
fun findWithin(@RequestParam("lat_north") latNorth: Double,
               @RequestParam("lon_west") lonWest: Double,
               @RequestParam("lat_south") latSouth: Double,
               @RequestParam("lon_east") lonEast: Double,
               page: Int, size: Int): Result<List<WalkItem>> {
    val rectangle = GeometryFactory().createPolygon(arrayOf(
            Coordinate(latNorth, lonWest),
            Coordinate(latNorth, lonEast),
            Coordinate(latSouth, lonEast),
            Coordinate(latSouth, lonWest),
            Coordinate(latNorth, lonWest)
    ))
    val wes = walkRepo.findWithin(rectangle, PageRequest.of(page, size))
    return Result.success(wes)
}

这个工程找到了。我试过把中国的gemo传给函数findWithin的area参数,也查询不出。

我的一些数据:

{
    "altitude": 1391.2064208984375,
    "latitude": 39.99987095,
    "longitude": 115.43765119
}
{
    "altitude": 45.00225830078125,
    "latitude": 40.00929495,
    "longitude": 116.38653411
}
{
    "altitude": 1075.603759765625,
    "latitude": 39.86346926,
    "longitude": 115.59797164
}

这是我想在国内查询的一些点

截图图片是我按地图角点位置查询出来的数据。

我相信您查询的是 latitude longitude 而不是 longitude latitude - 后一种组合是所有 SRS 中最常见的组合。您的坐标看起来很好,它们确实与您数据集中的中国多边形重叠。

查询 - 导入 shapefile 和您提供的坐标

WITH j AS (
  VALUES ('SRID=4326;POINT(115.43765119 39.99987095)'::GEOMETRY),
         ('SRID=4326;POINT(116.38653411 40.00929495)'::GEOMETRY),
         ('SRID=4326;POINT(115.59797164 39.86346926)'::GEOMETRY)
)
SELECT ST_AsText(column1), name FROM j, table_world
WHERE ST_Contains(geom,column1);

---------------------------------+-------
 POINT(115.43765119 39.99987095) | China
 POINT(116.38653411 40.00929495) | China
 POINT(115.59797164 39.86346926) | China

(3 Zeilen)