polygon: Polygons drawn on results (GeoJSON files) and point in polygon tests¶
moraine.api.polygon(源码)。每个名字也是 moraine.<name>(import moraine as mr)。
read_polygons
¶
Read the polygons of a GeoJSON file written by write_polygons or another program.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
GeoJSON file with Polygon or MultiPolygon features (holes are ignored) |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
list of polygons, each an (n_vertices, 2) float64 array of (x, y) with the closing vertex removed, and the coordinates, 'lonlat' or 'radar_grid' |
write_polygons
¶
Write polygons to a GeoJSON file (FeatureCollection of Polygon features).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
output GeoJSON file |
required |
polygons
|
list
|
polygons, each a list of (x, y) vertices, at least 3; the ring is closed in the file |
required |
coordinates
|
str
|
'lonlat' (x longitude, y latitude in degrees, standard GeoJSON) or 'radar_grid' (x range, y azimuth
pixel coordinates, stored as |
'lonlat'
|
polygons_contain
¶
Whether points are inside any of the polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons
|
list
|
polygons, each an (n_vertices, 2) array of (x, y) vertices, in the coordinates of |
required |
x
|
ndarray
|
x coordinates of the points, any shape |
required |
y
|
ndarray
|
y coordinates of the points, same shape as |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
bool, same shape as |
示例¶
构建本手册时运行;输出紧随每段代码。
import os
import tempfile
import numpy as np
import moraine as mr
square = [(14.10, 40.80), (14.15, 40.80), (14.15, 40.85), (14.10, 40.85)] # (longitude, latitude) vertices
path = os.path.join(tempfile.mkdtemp(), 'areas.geojson')
mr.write_polygons(path, [square]) # a GeoJSON FeatureCollection
polygons, coordinates = mr.read_polygons(path)
print(coordinates, polygons[0].shape)
lon = np.array([14.12, 14.20, np.nan])
lat = np.array([40.82, 40.82, 40.82])
print(mr.polygons_contain(polygons, lon, lat)) # inside, outside, nan
Polygons drawn in the viewer are saved in the same format; polygon-mask makes masks of rasters or point clouds
from them (moraine_coordinates tells longitude / latitude from radar grid coordinates).