<s> import unittest <EOL> from django . contrib . gis . gdal import OGRGeometry , OGRGeomType , OGRException , OGRIndexError , SpatialReference , CoordTransform , gdal_version <EOL> from django . contrib . gis . tests . geometries import * <EOL> class OGRGeomTest ( unittest . TestCase ) : <EOL> "<STR_LIT>" <EOL> def test00_geomtype ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> g = OGRGeomType ( <NUM_LIT:1> ) <EOL> g = OGRGeomType ( <NUM_LIT:7> ) <EOL> g = OGRGeomType ( '<STR_LIT>' ) <EOL> g = OGRGeomType ( '<STR_LIT>' ) <EOL> g = OGRGeomType ( '<STR_LIT>' ) <EOL> g = OGRGeomType ( '<STR_LIT>' ) <EOL> except : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , OGRGeomType . __init__ , <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , OGRGeomType . __init__ , '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , OGRGeomType . __init__ , <NUM_LIT:9> ) <EOL> self . assertEqual ( True , OGRGeomType ( <NUM_LIT:1> ) == OGRGeomType ( <NUM_LIT:1> ) ) <EOL> self . assertEqual ( True , OGRGeomType ( <NUM_LIT:7> ) == '<STR_LIT>' ) <EOL> self . assertEqual ( True , OGRGeomType ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> self . assertEqual ( False , OGRGeomType ( '<STR_LIT>' ) == <NUM_LIT:2> ) <EOL> self . assertEqual ( True , OGRGeomType ( '<STR_LIT>' ) == <NUM_LIT:0> ) <EOL> self . assertEqual ( True , OGRGeomType ( <NUM_LIT:6> ) == '<STR_LIT>' ) <EOL> self . assertEqual ( False , OGRGeomType ( <NUM_LIT:1> ) != OGRGeomType ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( True , OGRGeomType ( '<STR_LIT>' ) != OGRGeomType ( <NUM_LIT:6> ) ) <EOL> self . assertEqual ( '<STR_LIT>' , OGRGeomType ( '<STR_LIT>' ) . django ) <EOL> self . assertEqual ( None , OGRGeomType ( '<STR_LIT>' ) . django ) <EOL> self . assertEqual ( None , OGRGeomType ( '<STR_LIT:none>' ) . django ) <EOL> def test01a_wkt ( self ) : <EOL> "<STR_LIT>" <EOL> for g in wkt_out : <EOL> geom = OGRGeometry ( g . wkt ) <EOL> self . assertEqual ( g . wkt , geom . wkt ) <EOL> def test01b_gml ( self ) : <EOL> "<STR_LIT>" <EOL> for g in wkt_out : <EOL> geom = OGRGeometry ( g . wkt ) <EOL> self . assertEqual ( g . gml , geom . gml ) <EOL> def test01c_hex ( self ) : <EOL> "<STR_LIT>" <EOL> for g in hex_wkt : <EOL> geom1 = OGRGeometry ( g . wkt ) <EOL> self . assertEqual ( g . hex , geom1 . hex ) <EOL> geom2 = OGRGeometry ( g . hex ) <EOL> self . assertEqual ( geom1 , geom2 ) <EOL> def test01d_wkb ( self ) : <EOL> "<STR_LIT>" <EOL> from binascii import b2a_hex <EOL> for g in hex_wkt : <EOL> geom1 = OGRGeometry ( g . wkt ) <EOL> wkb = geom1 . wkb <EOL> self . assertEqual ( b2a_hex ( wkb ) . upper ( ) , g . hex ) <EOL> geom2 = OGRGeometry ( wkb ) <EOL> self . assertEqual ( geom1 , geom2 ) <EOL> def test01e_json ( self ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . gis . gdal . prototypes . geom import GEOJSON <EOL> if not GEOJSON : return <EOL> for g in json_geoms : <EOL> geom = OGRGeometry ( g . wkt ) <EOL> if not hasattr ( g , '<STR_LIT>' ) : <EOL> self . assertEqual ( g . json , geom . json ) <EOL> self . assertEqual ( g . json , geom . geojson ) <EOL> self . assertEqual ( OGRGeometry ( g . wkt ) , OGRGeometry ( geom . json ) ) <EOL> def test02_points ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for p in points : <EOL> if not hasattr ( p , '<STR_LIT:z>' ) : <EOL> pnt = OGRGeometry ( p . wkt ) <EOL> self . assertEqual ( <NUM_LIT:1> , pnt . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , pnt . geom_name ) <EOL> self . assertEqual ( p . x , pnt . x ) <EOL> self . assertEqual ( p . y , pnt . y ) <EOL> self . assertEqual ( ( p . x , p . y ) , pnt . tuple ) <EOL> def test03_multipoints ( self ) : <EOL> "<STR_LIT>" <EOL> for mp in multipoints : <EOL> mgeom1 = OGRGeometry ( mp . wkt ) <EOL> self . assertEqual ( <NUM_LIT:4> , mgeom1 . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , mgeom1 . geom_name ) <EOL> mgeom2 = OGRGeometry ( '<STR_LIT>' ) <EOL> mgeom3 = OGRGeometry ( '<STR_LIT>' ) <EOL> for g in mgeom1 : <EOL> mgeom2 . add ( g ) <EOL> mgeom3 . add ( g . wkt ) <EOL> self . assertEqual ( mgeom1 , mgeom2 ) <EOL> self . assertEqual ( mgeom1 , mgeom3 ) <EOL> self . assertEqual ( mp . points , mgeom2 . tuple ) <EOL> self . assertEqual ( mp . n_p , mgeom2 . point_count ) <EOL> def test04_linestring ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for ls in linestrings : <EOL> linestr = OGRGeometry ( ls . wkt ) <EOL> self . assertEqual ( <NUM_LIT:2> , linestr . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , linestr . geom_name ) <EOL> self . assertEqual ( ls . n_p , linestr . point_count ) <EOL> self . assertEqual ( ls . tup , linestr . tuple ) <EOL> self . assertEqual ( True , linestr == OGRGeometry ( ls . wkt ) ) <EOL> self . assertEqual ( True , linestr != prev ) <EOL> self . assertRaises ( OGRIndexError , linestr . __getitem__ , len ( linestr ) ) <EOL> prev = linestr <EOL> x = [ tmpx for tmpx , tmpy in ls . tup ] <EOL> y = [ tmpy for tmpx , tmpy in ls . tup ] <EOL> self . assertEqual ( x , linestr . x ) <EOL> self . assertEqual ( y , linestr . y ) <EOL> def test05_multilinestring ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for mls in multilinestrings : <EOL> mlinestr = OGRGeometry ( mls . wkt ) <EOL> self . assertEqual ( <NUM_LIT:5> , mlinestr . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , mlinestr . geom_name ) <EOL> self . assertEqual ( mls . n_p , mlinestr . point_count ) <EOL> self . assertEqual ( mls . tup , mlinestr . tuple ) <EOL> self . assertEqual ( True , mlinestr == OGRGeometry ( mls . wkt ) ) <EOL> self . assertEqual ( True , mlinestr != prev ) <EOL> prev = mlinestr <EOL> for ls in mlinestr : <EOL> self . assertEqual ( <NUM_LIT:2> , ls . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , ls . geom_name ) <EOL> self . assertRaises ( OGRIndexError , mlinestr . __getitem__ , len ( mlinestr ) ) <EOL> def test06_linearring ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for rr in linearrings : <EOL> lr = OGRGeometry ( rr . wkt ) <EOL> self . assertEqual ( '<STR_LIT>' , lr . geom_name ) <EOL> self . assertEqual ( rr . n_p , len ( lr ) ) <EOL> self . assertEqual ( True , lr == OGRGeometry ( rr . wkt ) ) <EOL> self . assertEqual ( True , lr != prev ) <EOL> prev = lr <EOL> def test07a_polygons ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for p in polygons : <EOL> poly = OGRGeometry ( p . wkt ) <EOL> self . assertEqual ( <NUM_LIT:3> , poly . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , poly . geom_name ) <EOL> self . assertEqual ( p . n_p , poly . point_count ) <EOL> self . assertEqual ( p . n_i + <NUM_LIT:1> , len ( poly ) ) <EOL> self . assertAlmostEqual ( p . area , poly . area , <NUM_LIT:9> ) <EOL> x , y = poly . centroid . tuple <EOL> self . assertAlmostEqual ( p . centroid [ <NUM_LIT:0> ] , x , <NUM_LIT:9> ) <EOL> self . assertAlmostEqual ( p . centroid [ <NUM_LIT:1> ] , y , <NUM_LIT:9> ) <EOL> self . assertEqual ( True , poly == OGRGeometry ( p . wkt ) ) <EOL> self . assertEqual ( True , poly != prev ) <EOL> if p . ext_ring_cs : <EOL> ring = poly [ <NUM_LIT:0> ] <EOL> self . assertEqual ( p . ext_ring_cs , ring . tuple ) <EOL> self . assertEqual ( p . ext_ring_cs , poly [ <NUM_LIT:0> ] . tuple ) <EOL> self . assertEqual ( len ( p . ext_ring_cs ) , ring . point_count ) <EOL> for r in poly : <EOL> self . assertEqual ( '<STR_LIT>' , r . geom_name ) <EOL> def test07b_closepolygons ( self ) : <EOL> "<STR_LIT>" <EOL> poly = OGRGeometry ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:8> , poly . point_count ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> c = poly . centroid <EOL> except OGRException : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> major , minor1 , minor2 = gdal_version ( ) . split ( '<STR_LIT:.>' ) <EOL> if major == '<STR_LIT:1>' : <EOL> iminor1 = int ( minor1 ) <EOL> if iminor1 < <NUM_LIT:4> or ( iminor1 == <NUM_LIT:4> and minor2 . startswith ( '<STR_LIT:1>' ) ) : return <EOL> poly . close_rings ( ) <EOL> self . assertEqual ( <NUM_LIT:10> , poly . point_count ) <EOL> self . assertEqual ( OGRGeometry ( '<STR_LIT>' ) , poly . centroid ) <EOL> def test08_multipolygons ( self ) : <EOL> "<STR_LIT>" <EOL> prev = OGRGeometry ( '<STR_LIT>' ) <EOL> for mp in multipolygons : <EOL> mpoly = OGRGeometry ( mp . wkt ) <EOL> self . assertEqual ( <NUM_LIT:6> , mpoly . geom_type ) <EOL> self . assertEqual ( '<STR_LIT>' , mpoly . geom_name ) <EOL> if mp . valid : <EOL> self . assertEqual ( mp . n_p , mpoly . point_count ) <EOL> self . assertEqual ( mp . num_geom , len ( mpoly ) ) <EOL> self . assertRaises ( OGRIndexError , mpoly . __getitem__ , len ( mpoly ) ) <EOL> for p in mpoly : <EOL> self . assertEqual ( '<STR_LIT>' , p . geom_name ) <EOL> self . assertEqual ( <NUM_LIT:3> , p . geom_type ) <EOL> self . assertEqual ( mpoly . wkt , OGRGeometry ( mp . wkt ) . wkt ) <EOL> def test09a_srs ( self ) : <EOL> "<STR_LIT>" <EOL> for mp in multipolygons : <EOL> sr = SpatialReference ( '<STR_LIT>' ) <EOL> mpoly = OGRGeometry ( mp . wkt , sr ) <EOL> self . assertEqual ( sr . wkt , mpoly . srs . wkt ) <EOL> klone = mpoly . clone ( ) <EOL> self . assertEqual ( sr . wkt , klone . srs . wkt ) <EOL> for poly in mpoly : <EOL> self . assertEqual ( sr . wkt , poly . srs . wkt ) <EOL> for ring in poly : <EOL> self . assertEqual ( sr . wkt , ring . srs . wkt ) <EOL> a , b = topology_geoms [ <NUM_LIT:0> ] <EOL> a , b = OGRGeometry ( a . wkt , sr ) , OGRGeometry ( b . wkt , sr ) <EOL> diff = a . difference ( b ) <EOL> union = a . union ( b ) <EOL> self . assertEqual ( sr . wkt , diff . srs . wkt ) <EOL> self . assertEqual ( sr . srid , union . srs . srid ) <EOL> mpoly = OGRGeometry ( mp . wkt , <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , mpoly . srid ) <EOL> mpoly . srs = SpatialReference ( <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , mpoly . srid ) <EOL> self . assertEqual ( '<STR_LIT>' , mpoly . srs . name ) <EOL> for poly in mpoly : <EOL> self . assertEqual ( mpoly . srs . wkt , poly . srs . wkt ) <EOL> poly . srs = <NUM_LIT> <EOL> for ring in poly : <EOL> self . assertEqual ( <NUM_LIT> , ring . srs . srid ) <EOL> self . assertEqual ( '<STR_LIT>' , ring . srs . name ) <EOL> ring . srs = str ( SpatialReference ( <NUM_LIT> ) ) <EOL> self . assertEqual ( <NUM_LIT> , ring . srs . srid ) <EOL> ring . srid = <NUM_LIT> <EOL> self . assertEqual ( '<STR_LIT>' , ring . srs . name ) <EOL> self . assertEqual ( <NUM_LIT> , ring . srid ) <EOL> def test09b_srs_transform ( self ) : <EOL> "<STR_LIT>" <EOL> orig = OGRGeometry ( '<STR_LIT>' , <NUM_LIT> ) <EOL> trans = OGRGeometry ( '<STR_LIT>' , <NUM_LIT> ) <EOL> t1 , t2 , t3 = orig . clone ( ) , orig . clone ( ) , orig . clone ( ) <EOL> t1 . transform ( trans . srid ) <EOL> t2 . transform ( SpatialReference ( '<STR_LIT>' ) ) <EOL> ct = CoordTransform ( SpatialReference ( '<STR_LIT>' ) , SpatialReference ( <NUM_LIT> ) ) <EOL> t3 . transform ( ct ) <EOL> k1 = orig . clone ( ) <EOL> k2 = k1 . transform ( trans . srid , clone = True ) <EOL> self . assertEqual ( k1 , orig ) <EOL> self . assertNotEqual ( k1 , k2 ) <EOL> prec = <NUM_LIT:3> <EOL> for p in ( t1 , t2 , t3 , k2 ) : <EOL> self . assertAlmostEqual ( trans . x , p . x , prec ) <EOL> self . assertAlmostEqual ( trans . y , p . y , prec ) <EOL> def test10_difference ( self ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( len ( topology_geoms ) ) : <EOL> g_tup = topology_geoms [ i ] <EOL> a = OGRGeometry ( g_tup [ <NUM_LIT:0> ] . wkt ) <EOL> b = OGRGeometry ( g_tup [ <NUM_LIT:1> ] . wkt ) <EOL> d1 = OGRGeometry ( diff_geoms [ i ] . wkt ) <EOL> d2 = a . difference ( b ) <EOL> self . assertEqual ( d1 , d2 ) <EOL> self . assertEqual ( d1 , a - b ) <EOL> a -= b <EOL> self . assertEqual ( d1 , a ) <EOL> def test11_intersection ( self ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( len ( topology_geoms ) ) : <EOL> g_tup = topology_geoms [ i ] <EOL> a = OGRGeometry ( g_tup [ <NUM_LIT:0> ] . wkt ) <EOL> b = OGRGeometry ( g_tup [ <NUM_LIT:1> ] . wkt ) <EOL> i1 = OGRGeometry ( intersect_geoms [ i ] . wkt ) <EOL> self . assertEqual ( True , a . intersects ( b ) ) <EOL> i2 = a . intersection ( b ) <EOL> self . assertEqual ( i1 , i2 ) <EOL> self . assertEqual ( i1 , a & b ) <EOL> a &= b <EOL> self . assertEqual ( i1 , a ) <EOL> def test12_symdifference ( self ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( len ( topology_geoms ) ) : <EOL> g_tup = topology_geoms [ i ] <EOL> a = OGRGeometry ( g_tup [ <NUM_LIT:0> ] . wkt ) <EOL> b = OGRGeometry ( g_tup [ <NUM_LIT:1> ] . wkt ) <EOL> d1 = OGRGeometry ( sdiff_geoms [ i ] . wkt ) <EOL> d2 = a . sym_difference ( b ) <EOL> self . assertEqual ( d1 , d2 ) <EOL> self . assertEqual ( d1 , a ^ b ) <EOL> a ^= b <EOL> self . assertEqual ( d1 , a ) <EOL> def test13_union ( self ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( len ( topology_geoms ) ) : <EOL> g_tup = topology_geoms [ i ] <EOL> a = OGRGeometry ( g_tup [ <NUM_LIT:0> ] . wkt ) <EOL> b = OGRGeometry ( g_tup [ <NUM_LIT:1> ] . wkt ) <EOL> u1 = OGRGeometry ( union_geoms [ i ] . wkt ) <EOL> u2 = a . union ( b ) <EOL> self . assertEqual ( u1 , u2 ) <EOL> self . assertEqual ( u1 , a | b ) <EOL> a |= b <EOL> self . assertEqual ( u1 , a ) <EOL> def test14_add ( self ) : <EOL> "<STR_LIT>" <EOL> mp = OGRGeometry ( '<STR_LIT>' ) <EOL> pnt = OGRGeometry ( '<STR_LIT>' ) <EOL> self . assertRaises ( OGRException , mp . add , pnt ) <EOL> for mp in multipolygons : <EOL> mpoly = OGRGeometry ( mp . wkt ) <EOL> mp1 = OGRGeometry ( '<STR_LIT>' ) <EOL> mp2 = OGRGeometry ( '<STR_LIT>' ) <EOL> mp3 = OGRGeometry ( '<STR_LIT>' ) <EOL> for poly in mpoly : <EOL> mp1 . add ( poly ) <EOL> mp2 . add ( poly . wkt ) <EOL> mp3 . add ( mpoly ) <EOL> for tmp in ( mp1 , mp2 , mp3 ) : self . assertEqual ( mpoly , tmp ) <EOL> def test15_extent ( self ) : <EOL> "<STR_LIT>" <EOL> mp = OGRGeometry ( '<STR_LIT>' ) <EOL> self . assertEqual ( ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> ) , mp . extent ) <EOL> poly = OGRGeometry ( polygons [ <NUM_LIT:3> ] . wkt ) <EOL> ring = poly . shell <EOL> x , y = ring . x , ring . y <EOL> xmin , ymin = min ( x ) , min ( y ) <EOL> xmax , ymax = max ( x ) , max ( y ) <EOL> self . assertEqual ( ( xmin , ymin , xmax , ymax ) , poly . extent ) <EOL> def suite ( ) : <EOL> s = unittest . TestSuite ( ) <EOL> s . addTest ( unittest . makeSuite ( OGRGeomTest ) ) <EOL> return s <EOL> def run ( verbosity = <NUM_LIT:2> ) : <EOL> unittest . TextTestRunner ( verbosity = verbosity ) . run ( suite ( ) ) </s>
