Ticket #11948: contrib-gis-linearref.patch

File contrib-gis-linearref.patch, 5.5 KB (added by novalis, 15 years ago)
  • django/contrib/gis/geos/geometry.py

     
    600600        "Clones this Geometry."
    601601        return GEOSGeometry(capi.geom_clone(self.ptr), srid=self.srid)
    602602
     603    def project(self, point):
     604        if not isinstance(point, Point):
     605            raise TypeError('locate_point argument must be a Point')
     606        if not isinstance(self, (LineString, MultiLineString)):
     607            raise TypeError('locate_point only works on LineString and MultiLineString geometries')
     608        return capi.geos_project(self.ptr, point.ptr)
     609
     610
     611    def interpolate(self, distance):
     612        if not isinstance(self, (LineString, MultiLineString)):
     613            raise TypeError('interpolate only works on LineString and MultiLineString geometries')
     614        return self._topology(capi.geos_interpolate(self.ptr, distance))
     615
     616    def project_normalized(self, point):
     617        if not isinstance(point, Point):
     618            raise TypeError('locate_point argument must be a Point')
     619        if not isinstance(self, (LineString, MultiLineString)):
     620            raise TypeError('locate_point only works on LineString and MultiLineString geometries')
     621        return capi.geos_project_normalized(self.ptr, point.ptr)
     622
     623
     624    def interpolate_normalized(self, distance):
     625        if not isinstance(self, (LineString, MultiLineString)):
     626            raise TypeError('interpolate only works on LineString and MultiLineString geometries')
     627        return self._topology(capi.geos_interpolate_normalized(self.ptr, distance))
     628
     629
    603630# Class mapping dictionary.  Has to be at the end to avoid import
    604631# conflicts with GEOSGeometry.
    605632from django.contrib.gis.geos.linestring import LineString, LinearRing
  • django/contrib/gis/geos/prototypes/__init__.py

     
    1313from django.contrib.gis.geos.prototypes.geom import from_hex, from_wkb, from_wkt, \
    1414    create_point, create_linestring, create_linearring, create_polygon, create_collection, \
    1515    destroy_geom, get_extring, get_intring, get_nrings, get_geomn, geom_clone, \
    16     geos_normalize, geos_type, geos_typeid, geos_get_srid, geos_set_srid, \
    17     get_dims, get_num_coords, get_num_geoms, \
    18     to_hex, to_wkb, to_wkt
     16    geos_normalize, geos_interpolate, geos_interpolate_normalized, geos_project, \
     17    geos_project_normalized, geos_type, geos_typeid, geos_get_srid, geos_set_srid, \
     18    get_dims, get_num_coords, get_num_geoms, to_hex, to_wkb, to_wkt
    1919
    2020# Miscellaneous routines.
    2121from django.contrib.gis.geos.prototypes.misc import geos_area, geos_distance, geos_length
  • django/contrib/gis/geos/prototypes/geom.py

     
    1 from ctypes import c_char_p, c_int, c_size_t, c_ubyte, c_uint, POINTER
    2 from django.contrib.gis.geos.libgeos import lgeos, CS_PTR, GEOM_PTR, PREPGEOM_PTR, GEOS_PREPARE
     1from ctypes import c_char_p, c_int, c_double, c_size_t, c_ubyte, c_uint, POINTER
     2from django.contrib.gis.geos.libgeos import geos_version_info, lgeos, CS_PTR, GEOM_PTR, PREPGEOM_PTR, GEOS_PREPARE
     3
    34from django.contrib.gis.geos.prototypes.errcheck import \
    45    check_geom, check_minus_one, check_sized_string, check_string, check_zero
    56
     
    116117geos_set_srid = lgeos.GEOSSetSRID
    117118geos_set_srid.argtypes = [GEOM_PTR, c_int]
    118119geos_set_srid.restype = None
     120
     121
     122# Linear referencing routines
     123
     124info = geos_version_info()
     125if info['version'] >= '3.2.0':
     126    geos_project = lgeos.GEOSProject
     127    geos_project.argtypes = [GEOM_PTR, GEOM_PTR]
     128    geos_project.restype = c_double
     129    geos_interpolate = geom_output(lgeos.GEOSInterpolate, [GEOM_PTR, c_double])
     130
     131    geos_project_normalized = lgeos.GEOSProjectNormalized
     132    geos_project_normalized.argtypes = [GEOM_PTR, GEOM_PTR]
     133    geos_project_normalized.restype = c_double
     134    geos_interpolate_normalized = geom_output(lgeos.GEOSInterpolateNormalized, [GEOM_PTR, c_double])
     135
  • django/contrib/gis/geos/tests/test_geos.py

     
    876876        for geom, merged in zip(ref_geoms, ref_merged):
    877877            self.assertEqual(merged, geom.merged)
    878878
     879    def test27_linearref(self):
     880        "Testing linear referencing"
     881
     882        info = geos_version_info()
     883        if info['version'] < '3.2.0':
     884            return #no linear referencing yet
     885           
     886        ls = fromstr('LINESTRING(0 0, 0 10, 10 10, 10 0)')
     887        mls = fromstr('MULTILINESTRING((0 0, 0 10), (10 0, 10 10))')
     888
     889        self.assertEqual(ls.project(Point(0, 20)), 10.0)
     890        self.assertEqual(ls.project(Point(7, 6)), 24)
     891
     892        self.assertEqual(ls.interpolate(10), Point(0, 10))
     893        self.assertEqual(ls.interpolate(24), Point(10, 6))
     894
     895        self.assertEqual(mls.project(Point(0, 20)), 10)
     896        self.assertEqual(mls.project(Point(7, 6)), 16)
     897
     898        self.assertEqual(mls.interpolate(9), Point(0, 9))
     899        self.assertEqual(mls.interpolate(17), Point(10, 7))
     900
     901
    879902def suite():
    880903    s = unittest.TestSuite()
    881904    s.addTest(unittest.makeSuite(GEOSTest))
Back to Top