| 1 |
from ctypes import POINTER, c_char_p, c_double, c_int, c_void_p |
| 2 |
|
| 3 |
from django.contrib.gis.gdal.envelope import OGREnvelope |
| 4 |
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal |
| 5 |
from django.contrib.gis.gdal.prototypes.errcheck import check_envelope |
| 6 |
from django.contrib.gis.gdal.prototypes.generation import ( |
| 7 |
bool_output, |
| 8 |
const_string_output, |
| 9 |
double_output, |
| 10 |
geom_output, |
| 11 |
int_output, |
| 12 |
srs_output, |
| 13 |
string_output, |
| 14 |
void_output, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
# ### Generation routines specific to this module ### |
| 19 |
def env_func(f, argtypes): |
| 20 |
"For getting OGREnvelopes." |
| 21 |
f.argtypes = argtypes |
| 22 |
f.restype = None |
| 23 |
f.errcheck = check_envelope |
| 24 |
return f |
| 25 |
|
| 26 |
|
| 27 |
def pnt_func(f): |
| 28 |
"For accessing point information." |
| 29 |
return double_output(f, [c_void_p, c_int]) |
| 30 |
|
| 31 |
|
| 32 |
def topology_func(f): |
| 33 |
f.argtypes = [c_void_p, c_void_p] |
| 34 |
f.restype = c_int |
| 35 |
f.errcheck = lambda result, func, cargs: bool(result) |
| 36 |
return f |
| 37 |
|
| 38 |
|
| 39 |
# ### OGR_G ctypes function prototypes ### |
| 40 |
|
| 41 |
# GeoJSON routines. |
| 42 |
from_json = geom_output(lgdal.OGR_G_CreateGeometryFromJson, [c_char_p]) |
| 43 |
to_json = string_output( |
| 44 |
lgdal.OGR_G_ExportToJson, [c_void_p], str_result=True, decoding="ascii" |
| 45 |
) |
| 46 |
to_kml = string_output( |
| 47 |
lgdal.OGR_G_ExportToKML, [c_void_p, c_char_p], str_result=True, decoding="ascii" |
| 48 |
) |
| 49 |
|
| 50 |
# GetX, GetY, GetZ all return doubles. |
| 51 |
getx = pnt_func(lgdal.OGR_G_GetX) |
| 52 |
gety = pnt_func(lgdal.OGR_G_GetY) |
| 53 |
getz = pnt_func(lgdal.OGR_G_GetZ) |
| 54 |
getm = pnt_func(lgdal.OGR_G_GetM) |
| 55 |
|
| 56 |
# Geometry creation routines. |
| 57 |
if GDAL_VERSION >= (3, 3): |
| 58 |
from_wkb = geom_output( |
| 59 |
lgdal.OGR_G_CreateFromWkbEx, |
| 60 |
[c_char_p, c_void_p, POINTER(c_void_p), c_int], |
| 61 |
offset=-2, |
| 62 |
) |
| 63 |
else: |
| 64 |
from_wkb = geom_output( |
| 65 |
lgdal.OGR_G_CreateFromWkb, |
| 66 |
[c_char_p, c_void_p, POINTER(c_void_p), c_int], |
| 67 |
offset=-2, |
| 68 |
) |
| 69 |
from_wkt = geom_output( |
| 70 |
lgdal.OGR_G_CreateFromWkt, |
| 71 |
[POINTER(c_char_p), c_void_p, POINTER(c_void_p)], |
| 72 |
offset=-1, |
| 73 |
) |
| 74 |
from_gml = geom_output(lgdal.OGR_G_CreateFromGML, [c_char_p]) |
| 75 |
create_geom = geom_output(lgdal.OGR_G_CreateGeometry, [c_int]) |
| 76 |
clone_geom = geom_output(lgdal.OGR_G_Clone, [c_void_p]) |
| 77 |
get_geom_ref = geom_output(lgdal.OGR_G_GetGeometryRef, [c_void_p, c_int]) |
| 78 |
get_boundary = geom_output(lgdal.OGR_G_GetBoundary, [c_void_p]) |
| 79 |
geom_convex_hull = geom_output(lgdal.OGR_G_ConvexHull, [c_void_p]) |
| 80 |
geom_diff = geom_output(lgdal.OGR_G_Difference, [c_void_p, c_void_p]) |
| 81 |
geom_intersection = geom_output(lgdal.OGR_G_Intersection, [c_void_p, c_void_p]) |
| 82 |
geom_sym_diff = geom_output(lgdal.OGR_G_SymmetricDifference, [c_void_p, c_void_p]) |
| 83 |
geom_union = geom_output(lgdal.OGR_G_Union, [c_void_p, c_void_p]) |
| 84 |
is_3d = bool_output(lgdal.OGR_G_Is3D, [c_void_p]) |
| 85 |
set_3d = void_output(lgdal.OGR_G_Set3D, [c_void_p, c_int], errcheck=False) |
| 86 |
is_measured = bool_output(lgdal.OGR_G_IsMeasured, [c_void_p]) |
| 87 |
set_measured = void_output(lgdal.OGR_G_SetMeasured, [c_void_p, c_int], errcheck=False) |
| 88 |
has_curve_geom = bool_output(lgdal.OGR_G_HasCurveGeometry, [c_void_p, c_int]) |
| 89 |
get_linear_geom = geom_output( |
| 90 |
lgdal.OGR_G_GetLinearGeometry, [c_void_p, c_double, POINTER(c_char_p)] |
| 91 |
) |
| 92 |
get_curve_geom = geom_output( |
| 93 |
lgdal.OGR_G_GetCurveGeometry, [c_void_p, POINTER(c_char_p)] |
| 94 |
) |
| 95 |
|
| 96 |
# Geometry modification routines. |
| 97 |
add_geom = void_output(lgdal.OGR_G_AddGeometry, [c_void_p, c_void_p]) |
| 98 |
import_wkt = void_output(lgdal.OGR_G_ImportFromWkt, [c_void_p, POINTER(c_char_p)]) |
| 99 |
|
| 100 |
# Destroys a geometry |
| 101 |
destroy_geom = void_output(lgdal.OGR_G_DestroyGeometry, [c_void_p], errcheck=False) |
| 102 |
|
| 103 |
# Geometry export routines. |
| 104 |
to_wkb = void_output( |
| 105 |
lgdal.OGR_G_ExportToWkb, None, errcheck=True |
| 106 |
) # special handling for WKB. |
| 107 |
to_iso_wkb = void_output(lgdal.OGR_G_ExportToIsoWkb, None, errcheck=True) |
| 108 |
to_wkt = string_output( |
| 109 |
lgdal.OGR_G_ExportToWkt, [c_void_p, POINTER(c_char_p)], decoding="ascii" |
| 110 |
) |
| 111 |
to_iso_wkt = string_output( |
| 112 |
lgdal.OGR_G_ExportToIsoWkt, [c_void_p, POINTER(c_char_p)], decoding="ascii" |
| 113 |
) |
| 114 |
to_gml = string_output( |
| 115 |
lgdal.OGR_G_ExportToGML, [c_void_p], str_result=True, decoding="ascii" |
| 116 |
) |
| 117 |
if GDAL_VERSION >= (3, 3): |
| 118 |
get_wkbsize = int_output(lgdal.OGR_G_WkbSizeEx, [c_void_p]) |
| 119 |
else: |
| 120 |
get_wkbsize = int_output(lgdal.OGR_G_WkbSize, [c_void_p]) |
| 121 |
|
| 122 |
# Geometry spatial-reference related routines. |
| 123 |
assign_srs = void_output( |
| 124 |
lgdal.OGR_G_AssignSpatialReference, [c_void_p, c_void_p], errcheck=False |
| 125 |
) |
| 126 |
get_geom_srs = srs_output(lgdal.OGR_G_GetSpatialReference, [c_void_p]) |
| 127 |
|
| 128 |
# Geometry properties |
| 129 |
get_area = double_output(lgdal.OGR_G_GetArea, [c_void_p]) |
| 130 |
get_centroid = void_output(lgdal.OGR_G_Centroid, [c_void_p, c_void_p]) |
| 131 |
get_dims = int_output(lgdal.OGR_G_GetDimension, [c_void_p]) |
| 132 |
get_coord_dim = int_output(lgdal.OGR_G_CoordinateDimension, [c_void_p]) |
| 133 |
set_coord_dim = void_output( |
| 134 |
lgdal.OGR_G_SetCoordinateDimension, [c_void_p, c_int], errcheck=False |
| 135 |
) |
| 136 |
is_empty = int_output( |
| 137 |
lgdal.OGR_G_IsEmpty, [c_void_p], errcheck=lambda result, func, cargs: bool(result) |
| 138 |
) |
| 139 |
|
| 140 |
get_geom_count = int_output(lgdal.OGR_G_GetGeometryCount, [c_void_p]) |
| 141 |
get_geom_name = const_string_output( |
| 142 |
lgdal.OGR_G_GetGeometryName, [c_void_p], decoding="ascii" |
| 143 |
) |
| 144 |
get_geom_type = int_output(lgdal.OGR_G_GetGeometryType, [c_void_p]) |
| 145 |
get_point_count = int_output(lgdal.OGR_G_GetPointCount, [c_void_p]) |
| 146 |
get_point = void_output( |
| 147 |
lgdal.OGR_G_GetPointZM, |
| 148 |
[ |
| 149 |
c_void_p, |
| 150 |
c_int, |
| 151 |
POINTER(c_double), |
| 152 |
POINTER(c_double), |
| 153 |
POINTER(c_double), |
| 154 |
POINTER(c_double), |
| 155 |
], |
| 156 |
errcheck=False, |
| 157 |
) |
| 158 |
geom_close_rings = void_output(lgdal.OGR_G_CloseRings, [c_void_p], errcheck=False) |
| 159 |
|
| 160 |
# Topology routines. |
| 161 |
ogr_contains = topology_func(lgdal.OGR_G_Contains) |
| 162 |
ogr_crosses = topology_func(lgdal.OGR_G_Crosses) |
| 163 |
ogr_disjoint = topology_func(lgdal.OGR_G_Disjoint) |
| 164 |
ogr_equals = topology_func(lgdal.OGR_G_Equals) |
| 165 |
ogr_intersects = topology_func(lgdal.OGR_G_Intersects) |
| 166 |
ogr_overlaps = topology_func(lgdal.OGR_G_Overlaps) |
| 167 |
ogr_touches = topology_func(lgdal.OGR_G_Touches) |
| 168 |
ogr_within = topology_func(lgdal.OGR_G_Within) |
| 169 |
|
| 170 |
# Transformation routines. |
| 171 |
geom_transform = void_output(lgdal.OGR_G_Transform, [c_void_p, c_void_p]) |
| 172 |
geom_transform_to = void_output(lgdal.OGR_G_TransformTo, [c_void_p, c_void_p]) |
| 173 |
|
| 174 |
# For retrieving the envelope of the geometry. |
| 175 |
get_envelope = env_func(lgdal.OGR_G_GetEnvelope, [c_void_p, POINTER(OGREnvelope)]) |