app/django/contrib/gis/gdal/prototypes/geom.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 from datetime import date
       
     2 from ctypes import c_char, c_char_p, c_double, c_int, c_ubyte, c_void_p, POINTER
       
     3 from django.contrib.gis.gdal.envelope import OGREnvelope
       
     4 from django.contrib.gis.gdal.libgdal import lgdal, gdal_version
       
     5 from django.contrib.gis.gdal.prototypes.errcheck import check_bool, check_envelope
       
     6 from django.contrib.gis.gdal.prototypes.generation import \
       
     7     const_string_output, double_output, geom_output, int_output, \
       
     8     srs_output, string_output, void_output
       
     9 
       
    10 # Some prototypes need to be aware of what version GDAL we have.
       
    11 major, minor = map(int, gdal_version().split('.')[:2])
       
    12 if major <= 1 and minor <= 4:
       
    13     GEOJSON = False
       
    14 else:
       
    15     GEOJSON = True
       
    16 
       
    17 ### Generation routines specific to this module ###
       
    18 def env_func(f, argtypes):
       
    19     "For getting OGREnvelopes."
       
    20     f.argtypes = argtypes
       
    21     f.restype = None
       
    22     f.errcheck = check_envelope
       
    23     return f
       
    24 
       
    25 def pnt_func(f):
       
    26     "For accessing point information."
       
    27     return double_output(f, [c_void_p, c_int])
       
    28 
       
    29 def topology_func(f):
       
    30     f.argtypes = [c_void_p, c_void_p]
       
    31     f.restype = c_int
       
    32     f.errchck = check_bool
       
    33     return f
       
    34 
       
    35 ### OGR_G ctypes function prototypes ###
       
    36 
       
    37 # GeoJSON routines, if supported.
       
    38 if GEOJSON:
       
    39     from_json = geom_output(lgdal.OGR_G_CreateGeometryFromJson, [c_char_p])
       
    40     to_json = string_output(lgdal.OGR_G_ExportToJson, [c_void_p], str_result=True)
       
    41 else:
       
    42     from_json = False
       
    43     to_json = False
       
    44 
       
    45 # GetX, GetY, GetZ all return doubles.
       
    46 getx = pnt_func(lgdal.OGR_G_GetX)
       
    47 gety = pnt_func(lgdal.OGR_G_GetY)
       
    48 getz = pnt_func(lgdal.OGR_G_GetZ)
       
    49     
       
    50 # Geometry creation routines.
       
    51 from_wkb = geom_output(lgdal.OGR_G_CreateFromWkb, [c_char_p, c_void_p, POINTER(c_void_p), c_int], offset=-2)
       
    52 from_wkt = geom_output(lgdal.OGR_G_CreateFromWkt, [POINTER(c_char_p), c_void_p, POINTER(c_void_p)], offset=-1)
       
    53 create_geom = geom_output(lgdal.OGR_G_CreateGeometry, [c_int])
       
    54 clone_geom = geom_output(lgdal.OGR_G_Clone, [c_void_p])
       
    55 get_geom_ref = geom_output(lgdal.OGR_G_GetGeometryRef, [c_void_p, c_int])
       
    56 get_boundary = geom_output(lgdal.OGR_G_GetBoundary, [c_void_p])
       
    57 geom_convex_hull = geom_output(lgdal.OGR_G_ConvexHull, [c_void_p])
       
    58 geom_diff = geom_output(lgdal.OGR_G_Difference, [c_void_p, c_void_p])
       
    59 geom_intersection = geom_output(lgdal.OGR_G_Intersection, [c_void_p, c_void_p])
       
    60 geom_sym_diff = geom_output(lgdal.OGR_G_SymmetricDifference, [c_void_p, c_void_p])
       
    61 geom_union = geom_output(lgdal.OGR_G_Union, [c_void_p, c_void_p])
       
    62 
       
    63 # Geometry modification routines.
       
    64 add_geom = void_output(lgdal.OGR_G_AddGeometry, [c_void_p, c_void_p])
       
    65 import_wkt = void_output(lgdal.OGR_G_ImportFromWkt, [c_void_p, POINTER(c_char_p)])
       
    66 
       
    67 # Destroys a geometry
       
    68 destroy_geom = void_output(lgdal.OGR_G_DestroyGeometry, [c_void_p], errcheck=False)
       
    69 
       
    70 # Geometry export routines.
       
    71 to_wkb = void_output(lgdal.OGR_G_ExportToWkb, None, errcheck=True) # special handling for WKB.
       
    72 to_wkt = string_output(lgdal.OGR_G_ExportToWkt, [c_void_p, POINTER(c_char_p)])
       
    73 to_gml = string_output(lgdal.OGR_G_ExportToGML, [c_void_p], str_result=True)
       
    74 get_wkbsize = int_output(lgdal.OGR_G_WkbSize, [c_void_p])
       
    75 
       
    76 # Geometry spatial-reference related routines.
       
    77 assign_srs = void_output(lgdal.OGR_G_AssignSpatialReference, [c_void_p, c_void_p], errcheck=False)
       
    78 get_geom_srs = srs_output(lgdal.OGR_G_GetSpatialReference, [c_void_p])
       
    79 
       
    80 # Geometry properties
       
    81 get_area = double_output(lgdal.OGR_G_GetArea, [c_void_p])
       
    82 get_centroid = void_output(lgdal.OGR_G_Centroid, [c_void_p, c_void_p])
       
    83 get_dims = int_output(lgdal.OGR_G_GetDimension, [c_void_p])
       
    84 get_coord_dims = int_output(lgdal.OGR_G_GetCoordinateDimension, [c_void_p])
       
    85 
       
    86 get_geom_count = int_output(lgdal.OGR_G_GetGeometryCount, [c_void_p])
       
    87 get_geom_name = const_string_output(lgdal.OGR_G_GetGeometryName, [c_void_p])
       
    88 get_geom_type = int_output(lgdal.OGR_G_GetGeometryType, [c_void_p])
       
    89 get_point_count = int_output(lgdal.OGR_G_GetPointCount, [c_void_p])
       
    90 get_point = void_output(lgdal.OGR_G_GetPoint, [c_void_p, c_int, POINTER(c_double), POINTER(c_double), POINTER(c_double)], errcheck=False)
       
    91 geom_close_rings = void_output(lgdal.OGR_G_CloseRings, [c_void_p], errcheck=False)
       
    92 
       
    93 # Topology routines.
       
    94 ogr_contains = topology_func(lgdal.OGR_G_Contains)
       
    95 ogr_crosses = topology_func(lgdal.OGR_G_Crosses)
       
    96 ogr_disjoint = topology_func(lgdal.OGR_G_Disjoint)
       
    97 ogr_equals = topology_func(lgdal.OGR_G_Equals)
       
    98 ogr_intersects = topology_func(lgdal.OGR_G_Intersects)
       
    99 ogr_overlaps = topology_func(lgdal.OGR_G_Overlaps)
       
   100 ogr_touches = topology_func(lgdal.OGR_G_Touches)
       
   101 ogr_within = topology_func(lgdal.OGR_G_Within)
       
   102 
       
   103 # Transformation routines.
       
   104 geom_transform = void_output(lgdal.OGR_G_Transform, [c_void_p, c_void_p])
       
   105 geom_transform_to = void_output(lgdal.OGR_G_TransformTo, [c_void_p, c_void_p])
       
   106 
       
   107 # For retrieving the envelope of the geometry.
       
   108 get_envelope = env_func(lgdal.OGR_G_GetEnvelope, [c_void_p, POINTER(OGREnvelope)])
       
   109