<s> from __future__ import unicode_literals <EOL> import os <EOL> import re <EOL> from unittest import skipUnless <EOL> from django . contrib . gis . db . models import Extent3D , Union <EOL> from django . contrib . gis . db . models . functions import ( <EOL> AsGeoJSON , AsKML , Length , Perimeter , Scale , Translate , <EOL> ) <EOL> from django . contrib . gis . gdal import HAS_GDAL <EOL> from django . contrib . gis . geos import GEOSGeometry , LineString , Point , Polygon <EOL> from django . test import TestCase , ignore_warnings , skipUnlessDBFeature <EOL> from django . utils . _os import upath <EOL> from django . utils . deprecation import RemovedInDjango20Warning <EOL> from . models import ( <EOL> City3D , Interstate2D , Interstate3D , InterstateProj2D , InterstateProj3D , <EOL> MultiPoint3D , Point2D , Point3D , Polygon2D , Polygon3D , <EOL> ) <EOL> if HAS_GDAL : <EOL> from django . contrib . gis . utils import LayerMapping , LayerMapError <EOL> data_path = os . path . realpath ( os . path . join ( os . path . dirname ( upath ( __file__ ) ) , '<STR_LIT:..>' , '<STR_LIT:data>' ) ) <EOL> city_file = os . path . join ( data_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> vrt_file = os . path . join ( data_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> city_data = ( <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> ) ) , <EOL> ) <EOL> city_dict = { name : coords for name , coords in city_data } <EOL> interstate_data = ( <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ) , <EOL> ) , <EOL> ) <EOL> bbox_data = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> ) <EOL> class Geo3DLoadingHelper ( object ) : <EOL> def _load_interstate_data ( self ) : <EOL> for name , line , exp_z in interstate_data : <EOL> line_3d = GEOSGeometry ( line , srid = <NUM_LIT> ) <EOL> line_2d = LineString ( [ l [ : <NUM_LIT:2> ] for l in line_3d . coords ] , srid = <NUM_LIT> ) <EOL> Interstate3D . objects . create ( name = name , line = line_3d ) <EOL> InterstateProj3D . objects . create ( name = name , line = line_3d ) <EOL> Interstate2D . objects . create ( name = name , line = line_2d ) <EOL> InterstateProj2D . objects . create ( name = name , line = line_2d ) <EOL> def _load_city_data ( self ) : <EOL> for name , pnt_data in city_data : <EOL> City3D . objects . create ( name = name , point = Point ( * pnt_data , srid = <NUM_LIT> ) ) <EOL> def _load_polygon_data ( self ) : <EOL> bbox_wkt , bbox_z = bbox_data <EOL> bbox_2d = GEOSGeometry ( bbox_wkt , srid = <NUM_LIT> ) <EOL> bbox_3d = Polygon ( tuple ( ( x , y , z ) for ( x , y ) , z in zip ( bbox_2d [ <NUM_LIT:0> ] . coords , bbox_z ) ) , srid = <NUM_LIT> ) <EOL> Polygon2D . objects . create ( name = '<STR_LIT>' , poly = bbox_2d ) <EOL> Polygon3D . objects . create ( name = '<STR_LIT>' , poly = bbox_3d ) <EOL> @ skipUnless ( HAS_GDAL , "<STR_LIT>" ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Geo3DTest ( Geo3DLoadingHelper , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_3d_hasz ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_interstate_data ( ) <EOL> for name , line , exp_z in interstate_data : <EOL> interstate = Interstate3D . objects . get ( name = name ) <EOL> interstate_proj = InterstateProj3D . objects . get ( name = name ) <EOL> for i in [ interstate , interstate_proj ] : <EOL> self . assertTrue ( i . line . hasz ) <EOL> self . assertEqual ( exp_z , tuple ( i . line . z ) ) <EOL> self . _load_city_data ( ) <EOL> for name , pnt_data in city_data : <EOL> city = City3D . objects . get ( name = name ) <EOL> z = pnt_data [ <NUM_LIT:2> ] <EOL> self . assertTrue ( city . point . hasz ) <EOL> self . assertEqual ( z , city . point . z ) <EOL> def test_3d_polygons ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_polygon_data ( ) <EOL> p3d = Polygon3D . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertTrue ( p3d . poly . hasz ) <EOL> self . assertIsInstance ( p3d . poly , Polygon ) <EOL> self . assertEqual ( p3d . poly . srid , <NUM_LIT> ) <EOL> def test_3d_layermapping ( self ) : <EOL> """<STR_LIT>""" <EOL> point_mapping = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mpoint_mapping = { '<STR_LIT>' : '<STR_LIT>' } <EOL> lm = LayerMapping ( Point2D , vrt_file , point_mapping , transform = False ) <EOL> lm . save ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , Point2D . objects . count ( ) ) <EOL> with self . assertRaises ( LayerMapError ) : <EOL> LayerMapping ( Point3D , city_file , point_mapping , transform = False ) <EOL> lm = LayerMapping ( Point3D , vrt_file , point_mapping , transform = False ) <EOL> lm . save ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , Point3D . objects . count ( ) ) <EOL> lm = LayerMapping ( MultiPoint3D , vrt_file , mpoint_mapping , transform = False ) <EOL> lm . save ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , MultiPoint3D . objects . count ( ) ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> def test_kml ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> h = City3D . objects . kml ( precision = <NUM_LIT:6> ) . get ( name = '<STR_LIT>' ) <EOL> ref_kml_regex = re . compile ( r'<STR_LIT>' ) <EOL> self . assertTrue ( ref_kml_regex . match ( h . kml ) ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> def test_geojson ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> h = City3D . objects . geojson ( precision = <NUM_LIT:6> ) . get ( name = '<STR_LIT>' ) <EOL> ref_json_regex = re . compile ( r'<STR_LIT>' ) <EOL> self . assertTrue ( ref_json_regex . match ( h . geojson ) ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_union ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> ref_ewkt = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> ref_union = GEOSGeometry ( ref_ewkt ) <EOL> union = City3D . objects . aggregate ( Union ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> self . assertTrue ( union . hasz ) <EOL> self . assertSetEqual ( { p . ewkt for p in ref_union } , { p . ewkt for p in union } ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_extent ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> ref_extent3d = ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> extent = City3D . objects . aggregate ( Extent3D ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> def check_extent3d ( extent3d , tol = <NUM_LIT:6> ) : <EOL> for ref_val , ext_val in zip ( ref_extent3d , extent3d ) : <EOL> self . assertAlmostEqual ( ref_val , ext_val , tol ) <EOL> check_extent3d ( extent ) <EOL> self . assertIsNone ( City3D . objects . none ( ) . aggregate ( Extent3D ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_perimeter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_polygon_data ( ) <EOL> ref_perim_3d = <NUM_LIT> <EOL> ref_perim_2d = <NUM_LIT> <EOL> tol = <NUM_LIT:6> <EOL> self . assertAlmostEqual ( ref_perim_2d , <EOL> Polygon2D . objects . perimeter ( ) . get ( name = '<STR_LIT>' ) . perimeter . m , <EOL> tol ) <EOL> self . assertAlmostEqual ( ref_perim_3d , <EOL> Polygon3D . objects . perimeter ( ) . get ( name = '<STR_LIT>' ) . perimeter . m , <EOL> tol ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_length ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_interstate_data ( ) <EOL> tol = <NUM_LIT:3> <EOL> ref_length_2d = <NUM_LIT> <EOL> ref_length_3d = <NUM_LIT> <EOL> self . assertAlmostEqual ( ref_length_2d , <EOL> Interstate2D . objects . length ( ) . get ( name = '<STR_LIT>' ) . length . m , <EOL> tol ) <EOL> self . assertAlmostEqual ( ref_length_3d , <EOL> Interstate3D . objects . length ( ) . get ( name = '<STR_LIT>' ) . length . m , <EOL> tol ) <EOL> ref_length_2d = <NUM_LIT> <EOL> ref_length_3d = <NUM_LIT> <EOL> self . assertAlmostEqual ( ref_length_2d , <EOL> InterstateProj2D . objects . length ( ) . get ( name = '<STR_LIT>' ) . length . m , <EOL> tol ) <EOL> self . assertAlmostEqual ( ref_length_3d , <EOL> InterstateProj3D . objects . length ( ) . get ( name = '<STR_LIT>' ) . length . m , <EOL> tol ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_scale ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> zscales = ( - <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT> ) <EOL> for zscale in zscales : <EOL> for city in City3D . objects . scale ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , zscale ) : <EOL> self . assertEqual ( city_dict [ city . name ] [ <NUM_LIT:2> ] * zscale , city . scale . z ) <EOL> @ ignore_warnings ( category = RemovedInDjango20Warning ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_translate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> ztranslations = ( <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ) <EOL> for ztrans in ztranslations : <EOL> for city in City3D . objects . translate ( <NUM_LIT:0> , <NUM_LIT:0> , ztrans ) : <EOL> self . assertEqual ( city_dict [ city . name ] [ <NUM_LIT:2> ] + ztrans , city . translate . z ) <EOL> @ skipUnless ( HAS_GDAL , "<STR_LIT>" ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Geo3DFunctionsTests ( Geo3DLoadingHelper , TestCase ) : <EOL> def test_kml ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> h = City3D . objects . annotate ( kml = AsKML ( '<STR_LIT>' , precision = <NUM_LIT:6> ) ) . get ( name = '<STR_LIT>' ) <EOL> ref_kml_regex = re . compile ( r'<STR_LIT>' ) <EOL> self . assertTrue ( ref_kml_regex . match ( h . kml ) ) <EOL> def test_geojson ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> h = City3D . objects . annotate ( geojson = AsGeoJSON ( '<STR_LIT>' , precision = <NUM_LIT:6> ) ) . get ( name = '<STR_LIT>' ) <EOL> ref_json_regex = re . compile ( r'<STR_LIT>' ) <EOL> self . assertTrue ( ref_json_regex . match ( h . geojson ) ) <EOL> def test_perimeter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_polygon_data ( ) <EOL> ref_perim_3d = <NUM_LIT> <EOL> ref_perim_2d = <NUM_LIT> <EOL> tol = <NUM_LIT:6> <EOL> poly2d = Polygon2D . objects . annotate ( perimeter = Perimeter ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_perim_2d , poly2d . perimeter . m , tol ) <EOL> poly3d = Polygon3D . objects . annotate ( perimeter = Perimeter ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_perim_3d , poly3d . perimeter . m , tol ) <EOL> def test_length ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_interstate_data ( ) <EOL> tol = <NUM_LIT:3> <EOL> ref_length_2d = <NUM_LIT> <EOL> ref_length_3d = <NUM_LIT> <EOL> inter2d = Interstate2D . objects . annotate ( length = Length ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_length_2d , inter2d . length . m , tol ) <EOL> inter3d = Interstate3D . objects . annotate ( length = Length ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_length_3d , inter3d . length . m , tol ) <EOL> ref_length_2d = <NUM_LIT> <EOL> ref_length_3d = <NUM_LIT> <EOL> inter2d = InterstateProj2D . objects . annotate ( length = Length ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_length_2d , inter2d . length . m , tol ) <EOL> inter3d = InterstateProj3D . objects . annotate ( length = Length ( '<STR_LIT>' ) ) . get ( name = '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( ref_length_3d , inter3d . length . m , tol ) <EOL> def test_scale ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> zscales = ( - <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT> ) <EOL> for zscale in zscales : <EOL> for city in City3D . objects . annotate ( scale = Scale ( '<STR_LIT>' , <NUM_LIT:1.0> , <NUM_LIT:1.0> , zscale ) ) : <EOL> self . assertEqual ( city_dict [ city . name ] [ <NUM_LIT:2> ] * zscale , city . scale . z ) <EOL> def test_translate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _load_city_data ( ) <EOL> ztranslations = ( <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ) <EOL> for ztrans in ztranslations : <EOL> for city in City3D . objects . annotate ( translate = Translate ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:0> , ztrans ) ) : <EOL> self . assertEqual ( city_dict [ city . name ] [ <NUM_LIT:2> ] + ztrans , city . translate . z ) </s>
