PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/gdal/prototypes/geom.py

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