<s> import os , unittest <EOL> from decimal import Decimal <EOL> from django . db . models import Q <EOL> from django . contrib . gis . gdal import DataSource <EOL> from django . contrib . gis . geos import GEOSGeometry , Point , LineString <EOL> from django . contrib . gis . measure import D <EOL> from django . contrib . gis . tests . utils import oracle , postgis , spatialite , no_oracle , no_spatialite <EOL> from models import AustraliaCity , Interstate , SouthTexasInterstate , SouthTexasCity , SouthTexasCityFt , CensusZipcode , SouthTexasZipcode <EOL> from data import au_cities , interstates , stx_interstates , stx_cities , stx_zips <EOL> class DistanceTest ( unittest . TestCase ) : <EOL> stx_pnt = GEOSGeometry ( '<STR_LIT>' , <NUM_LIT> ) <EOL> au_pnt = GEOSGeometry ( '<STR_LIT>' , <NUM_LIT> ) <EOL> def get_names ( self , qs ) : <EOL> cities = [ c . name for c in qs ] <EOL> cities . sort ( ) <EOL> return cities <EOL> def test01_init ( self ) : <EOL> "<STR_LIT>" <EOL> def load_cities ( city_model , data_tup ) : <EOL> for name , x , y in data_tup : <EOL> city_model ( name = name , point = Point ( x , y , srid = <NUM_LIT> ) ) . save ( ) <EOL> def load_interstates ( imodel , data_tup ) : <EOL> for name , wkt in data_tup : <EOL> imodel ( name = name , path = wkt ) . save ( ) <EOL> load_cities ( SouthTexasCity , stx_cities ) <EOL> load_cities ( SouthTexasCityFt , stx_cities ) <EOL> load_cities ( AustraliaCity , au_cities ) <EOL> self . assertEqual ( <NUM_LIT:9> , SouthTexasCity . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:9> , SouthTexasCityFt . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:11> , AustraliaCity . objects . count ( ) ) <EOL> for name , wkt in stx_zips : <EOL> poly = GEOSGeometry ( wkt , srid = <NUM_LIT> ) <EOL> SouthTexasZipcode ( name = name , poly = poly ) . save ( ) <EOL> CensusZipcode ( name = name , poly = poly ) . save ( ) <EOL> self . assertEqual ( <NUM_LIT:4> , SouthTexasZipcode . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , CensusZipcode . objects . count ( ) ) <EOL> load_interstates ( Interstate , interstates ) <EOL> load_interstates ( SouthTexasInterstate , stx_interstates ) <EOL> self . assertEqual ( <NUM_LIT:1> , Interstate . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , SouthTexasInterstate . objects . count ( ) ) <EOL> @ no_spatialite <EOL> def test02_dwithin ( self ) : <EOL> "<STR_LIT>" <EOL> tx_dists = [ ( <NUM_LIT> , <NUM_LIT> ) , D ( km = <NUM_LIT:7> ) , D ( mi = <NUM_LIT> ) ] <EOL> au_dists = [ ( <NUM_LIT:0.5> , <NUM_LIT> ) , D ( km = <NUM_LIT:32> ) , D ( mi = <NUM_LIT> ) ] <EOL> tx_cities = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> au_cities = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for dist in tx_dists : <EOL> if isinstance ( dist , tuple ) : dist1 , dist2 = dist <EOL> else : dist1 = dist2 = dist <EOL> qs1 = SouthTexasCity . objects . filter ( point__dwithin = ( self . stx_pnt , dist1 ) ) <EOL> qs2 = SouthTexasCityFt . objects . filter ( point__dwithin = ( self . stx_pnt , dist2 ) ) <EOL> for qs in qs1 , qs2 : <EOL> self . assertEqual ( tx_cities , self . get_names ( qs ) ) <EOL> for dist in au_dists : <EOL> if isinstance ( dist , D ) and not oracle : type_error = True <EOL> else : type_error = False <EOL> if isinstance ( dist , tuple ) : <EOL> if oracle : dist = dist [ <NUM_LIT:1> ] <EOL> else : dist = dist [ <NUM_LIT:0> ] <EOL> qs = AustraliaCity . objects . order_by ( '<STR_LIT:name>' ) <EOL> if type_error : <EOL> self . assertRaises ( TypeError , AustraliaCity . objects . filter , point__dwithin = ( self . au_pnt , dist ) ) <EOL> else : <EOL> self . assertEqual ( au_cities , self . get_names ( qs . filter ( point__dwithin = ( self . au_pnt , dist ) ) ) ) <EOL> def test03a_distance_method ( self ) : <EOL> "<STR_LIT>" <EOL> lagrange = GEOSGeometry ( '<STR_LIT>' , <NUM_LIT> ) <EOL> m_distances = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> ft_distances = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> dist1 = SouthTexasCity . objects . distance ( lagrange , field_name = '<STR_LIT>' ) <EOL> dist2 = SouthTexasCity . objects . distance ( lagrange ) <EOL> if spatialite or oracle : <EOL> dist_qs = [ dist1 , dist2 ] <EOL> else : <EOL> dist3 = SouthTexasCityFt . objects . distance ( lagrange . ewkt ) <EOL> dist4 = SouthTexasCityFt . objects . distance ( lagrange ) <EOL> dist_qs = [ dist1 , dist2 , dist3 , dist4 ] <EOL> if oracle : tol = <NUM_LIT:2> <EOL> else : tol = <NUM_LIT:5> <EOL> for qs in dist_qs : <EOL> for i , c in enumerate ( qs ) : <EOL> self . assertAlmostEqual ( m_distances [ i ] , c . distance . m , tol ) <EOL> self . assertAlmostEqual ( ft_distances [ i ] , c . distance . survey_ft , tol ) <EOL> @ no_spatialite <EOL> def test03b_distance_method ( self ) : <EOL> "<STR_LIT>" <EOL> if oracle : tol = <NUM_LIT:2> <EOL> else : tol = <NUM_LIT:5> <EOL> hillsdale = AustraliaCity . objects . get ( name = '<STR_LIT>' ) <EOL> if not oracle : <EOL> self . assertRaises ( ValueError , AustraliaCity . objects . distance , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , AustraliaCity . objects . distance , LineString ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> spheroid_distances = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> sphere_distances = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> qs = AustraliaCity . objects . exclude ( id = hillsdale . id ) . distance ( hillsdale . point , spheroid = True ) <EOL> for i , c in enumerate ( qs ) : <EOL> self . assertAlmostEqual ( spheroid_distances [ i ] , c . distance . m , tol ) <EOL> if postgis : <EOL> qs = AustraliaCity . objects . exclude ( id = hillsdale . id ) . distance ( hillsdale . point ) <EOL> for i , c in enumerate ( qs ) : <EOL> self . assertAlmostEqual ( sphere_distances [ i ] , c . distance . m , tol ) <EOL> @ no_oracle <EOL> def test03c_distance_method ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertRaises ( ValueError , CensusZipcode . objects . distance , self . stx_pnt ) <EOL> z = SouthTexasZipcode . objects . get ( name = '<STR_LIT>' ) <EOL> dists_m = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> buf1 = z . poly . centroid . buffer ( <NUM_LIT:100> ) <EOL> buf2 = buf1 . transform ( <NUM_LIT> , clone = True ) <EOL> ref_zips = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for buf in [ buf1 , buf2 ] : <EOL> qs = CensusZipcode . objects . exclude ( name = '<STR_LIT>' ) . transform ( <NUM_LIT> ) . distance ( buf ) <EOL> self . assertEqual ( ref_zips , self . get_names ( qs ) ) <EOL> for i , z in enumerate ( qs ) : <EOL> self . assertAlmostEqual ( z . distance . m , dists_m [ i ] , <NUM_LIT:5> ) <EOL> def test04_distance_lookups ( self ) : <EOL> "<STR_LIT>" <EOL> qs1 = SouthTexasCity . objects . filter ( point__distance_gte = ( self . stx_pnt , D ( km = <NUM_LIT:7> ) ) ) . filter ( point__distance_lte = ( self . stx_pnt , D ( km = <NUM_LIT:20> ) ) ) <EOL> if spatialite or oracle : <EOL> dist_qs = ( qs1 , ) <EOL> else : <EOL> qs2 = SouthTexasCityFt . objects . filter ( point__distance_gte = ( self . stx_pnt , D ( km = <NUM_LIT:7> ) ) ) . filter ( point__distance_lte = ( self . stx_pnt , D ( km = <NUM_LIT:20> ) ) ) <EOL> dist_qs = ( qs1 , qs2 ) <EOL> for qs in dist_qs : <EOL> cities = self . get_names ( qs ) <EOL> self . assertEqual ( cities , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> z = SouthTexasZipcode . objects . get ( name = '<STR_LIT>' ) <EOL> qs = SouthTexasZipcode . objects . exclude ( name = '<STR_LIT>' ) . filter ( poly__distance_lte = ( z . poly , D ( m = <NUM_LIT> ) ) ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' ] , self . get_names ( qs ) ) <EOL> qs = SouthTexasZipcode . objects . exclude ( name = '<STR_LIT>' ) . filter ( poly__distance_lte = ( z . poly , D ( m = <NUM_LIT> ) ) ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , self . get_names ( qs ) ) <EOL> @ no_spatialite <EOL> def test05_geodetic_distance_lookups ( self ) : <EOL> "<STR_LIT>" <EOL> if not oracle : <EOL> mp = GEOSGeometry ( '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , <EOL> AustraliaCity . objects . filter ( point__distance_lte = ( mp , D ( km = <NUM_LIT:100> ) ) ) ) <EOL> self . assertRaises ( ValueError , <EOL> AustraliaCity . objects . filter , point__distance_lte = ( '<STR_LIT>' , D ( km = <NUM_LIT:100> ) , '<STR_LIT>' , '<STR_LIT:4>' ) ) <EOL> self . assertRaises ( ValueError , <EOL> AustraliaCity . objects . filter , point__distance_lte = ( '<STR_LIT>' , ) ) <EOL> hobart = AustraliaCity . objects . get ( name = '<STR_LIT>' ) <EOL> qs = AustraliaCity . objects . exclude ( name = '<STR_LIT>' ) . filter ( point__distance_lte = ( hobart . point , D ( mi = <NUM_LIT> ) ) ) <EOL> cities = self . get_names ( qs ) <EOL> self . assertEqual ( cities , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> wollongong = AustraliaCity . objects . get ( name = '<STR_LIT>' ) <EOL> d1 , d2 = D ( yd = <NUM_LIT> ) , D ( nm = <NUM_LIT> ) <EOL> gq1 = Q ( point__distance_lte = ( wollongong . point , d1 ) ) <EOL> gq2 = Q ( point__distance_gte = ( wollongong . point , d2 ) ) <EOL> qs1 = AustraliaCity . objects . exclude ( name = '<STR_LIT>' ) . filter ( gq1 | gq2 ) <EOL> if postgis : <EOL> gq3 = Q ( point__distance_lte = ( wollongong . point , d1 , '<STR_LIT>' ) ) <EOL> gq4 = Q ( point__distance_gte = ( wollongong . point , d2 , '<STR_LIT>' ) ) <EOL> qs2 = AustraliaCity . objects . exclude ( name = '<STR_LIT>' ) . filter ( gq3 | gq4 ) <EOL> querysets = [ qs1 , qs2 ] <EOL> else : <EOL> querysets = [ qs1 ] <EOL> for qs in querysets : <EOL> cities = self . get_names ( qs ) <EOL> self . assertEqual ( cities , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test06_area ( self ) : <EOL> "<STR_LIT>" <EOL> area_sq_m = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> tol = <NUM_LIT:2> <EOL> for i , z in enumerate ( SouthTexasZipcode . objects . area ( ) ) : <EOL> self . assertAlmostEqual ( area_sq_m [ i ] , z . area . sq_m , tol ) <EOL> def test07_length ( self ) : <EOL> "<STR_LIT>" <EOL> len_m1 = <NUM_LIT> <EOL> len_m2 = <NUM_LIT> <EOL> if spatialite : <EOL> self . assertRaises ( ValueError , Interstate . objects . length ) <EOL> else : <EOL> qs = Interstate . objects . length ( ) <EOL> if oracle : tol = <NUM_LIT:2> <EOL> else : tol = <NUM_LIT:5> <EOL> self . assertAlmostEqual ( len_m1 , qs [ <NUM_LIT:0> ] . length . m , tol ) <EOL> i10 = SouthTexasInterstate . objects . length ( ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( len_m2 , i10 . length . m , <NUM_LIT:2> ) <EOL> @ no_spatialite <EOL> def test08_perimeter ( self ) : <EOL> "<STR_LIT>" <EOL> perim_m = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> if oracle : tol = <NUM_LIT:2> <EOL> else : tol = <NUM_LIT:7> <EOL> for i , z in enumerate ( SouthTexasZipcode . objects . perimeter ( ) ) : <EOL> self . assertAlmostEqual ( perim_m [ i ] , z . perimeter . m , tol ) <EOL> for i , c in enumerate ( SouthTexasCity . objects . perimeter ( model_att = '<STR_LIT>' ) ) : <EOL> self . assertEqual ( <NUM_LIT:0> , c . perim . m ) <EOL> def suite ( ) : <EOL> s = unittest . TestSuite ( ) <EOL> s . addTest ( unittest . makeSuite ( DistanceTest ) ) <EOL> return s </s>
