forked from geopandas/pyogrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_geometry.pyx
114 lines (92 loc) · 3.42 KB
/
_geometry.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import warnings
from pyogrio._ogr cimport *
from pyogrio._err cimport *
from pyogrio.errors import UnsupportedGeometryTypeError
# Mapping of OGR integer geometry types to GeoJSON type names.
GEOMETRY_TYPES = {
wkbUnknown: 'Unknown',
wkbPoint: 'Point',
wkbLineString: 'LineString',
wkbPolygon: 'Polygon',
wkbMultiPoint: 'MultiPoint',
wkbMultiLineString: 'MultiLineString',
wkbMultiPolygon: 'MultiPolygon',
wkbGeometryCollection: 'GeometryCollection',
wkbNone: None,
wkbLinearRing: 'LinearRing',
# WARNING: Measured types are not supported in GEOS and downstream uses
# these are stripped automatically to their corresponding 2D / 3D types
wkbPointM: 'PointM',
wkbLineStringM: 'Measured LineString',
wkbPolygonM: 'Measured Polygon',
wkbMultiPointM: 'Measured MultiPoint',
wkbMultiLineStringM: 'Measured MultiLineString',
wkbMultiPolygonM: 'Measured MultiPolygon',
wkbGeometryCollectionM: 'Measured GeometryCollection',
wkbPointZM: 'Measured 3D Point',
wkbLineStringZM: 'Measured 3D LineString',
wkbPolygonZM: 'Measured 3D Polygon',
wkbMultiPointZM: 'Measured 3D MultiPoint',
wkbMultiLineStringZM: 'Measured 3D MultiLineString',
wkbMultiPolygonZM: 'Measured 3D MultiPolygon',
wkbGeometryCollectionZM: 'Measured 3D GeometryCollection',
wkbPoint25D: '2.5D Point',
wkbLineString25D: '2.5D LineString',
wkbPolygon25D: '2.5D Polygon',
wkbMultiPoint25D: '2.5D MultiPoint',
wkbMultiLineString25D: '2.5D MultiLineString',
wkbMultiPolygon25D: '2.5D MultiPolygon',
wkbGeometryCollection25D: '2.5D GeometryCollection',
}
GEOMETRY_TYPE_CODES = {v:k for k, v in GEOMETRY_TYPES.items()}
# 2.5D also represented using negative numbers not enumerated above
GEOMETRY_TYPES.update({
-2147483647: '2.5D Point',
-2147483646: '2.5D LineString',
-2147483645: '2.5D Polygon',
-2147483644: '2.5D MultiPoint',
-2147483643: '2.5D MultiLineString',
-2147483642: '2.5D MultiPolygon',
-2147483641: '2.5D GeometryCollection',
})
cdef str get_geometry_type(void *ogr_layer):
"""Get geometry type for layer.
Parameters
----------
ogr_layer : pointer to open OGR layer
Returns
-------
str
geometry type
"""
cdef void *cogr_featuredef = NULL
cdef OGRwkbGeometryType ogr_type
ogr_featuredef = OGR_L_GetLayerDefn(ogr_layer)
if ogr_featuredef == NULL:
raise ValueError("Null feature definition")
ogr_type = OGR_FD_GetGeomType(ogr_featuredef)
if ogr_type not in GEOMETRY_TYPES:
raise UnsupportedGeometryTypeError(ogr_type)
if OGR_GT_HasM(ogr_type):
original_type = GEOMETRY_TYPES[ogr_type]
# Downgrade the type to 2D / 3D
ogr_type = OGR_GT_SetModifier(ogr_type, OGR_GT_HasZ(ogr_type), 0)
# TODO: review; this might be annoying...
warnings.warn(
"Measured (M) geometry types are not supported. "
f"Original type '{original_type}' "
f"is converted to '{GEOMETRY_TYPES[ogr_type]}'")
return GEOMETRY_TYPES[ogr_type]
cdef int get_geometry_type_code(str geometry_type):
"""Get geometry type code for string geometry type.
Parameters
----------
geometry_type : str
Returns
-------
int
geometry type code
"""
if geometry_type not in GEOMETRY_TYPE_CODES:
raise UnsupportedGeometryTypeError(geometry_type)
return GEOMETRY_TYPE_CODES[geometry_type]