<s> from django . contrib . gis . geos import prototypes as capi <EOL> from django . contrib . gis . geos . coordseq import GEOSCoordSeq <EOL> from django . contrib . gis . geos . error import GEOSException <EOL> from django . contrib . gis . geos . geometry import GEOSGeometry , LinearGeometryMixin <EOL> from django . contrib . gis . geos . point import Point <EOL> from django . contrib . gis . shortcuts import numpy <EOL> from django . utils . six . moves import range <EOL> class LineString ( LinearGeometryMixin , GEOSGeometry ) : <EOL> _init_func = capi . create_linestring <EOL> _minlength = <NUM_LIT:2> <EOL> has_cs = True <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> coords = args [ <NUM_LIT:0> ] <EOL> else : <EOL> coords = args <EOL> if not ( isinstance ( coords , ( tuple , list ) ) or numpy and isinstance ( coords , numpy . ndarray ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> srid = kwargs . get ( '<STR_LIT>' ) <EOL> ncoords = len ( coords ) <EOL> if not ncoords : <EOL> super ( LineString , self ) . __init__ ( self . _init_func ( None ) , srid = srid ) <EOL> return <EOL> if ncoords < self . _minlength : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . _minlength , <EOL> ncoords , <EOL> ) <EOL> ) <EOL> if isinstance ( coords , ( tuple , list ) ) : <EOL> ndim = None <EOL> for coord in coords : <EOL> if not isinstance ( coord , ( tuple , list , Point ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if ndim is None : <EOL> ndim = len ( coord ) <EOL> self . _checkdim ( ndim ) <EOL> elif len ( coord ) != ndim : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> numpy_coords = False <EOL> else : <EOL> shape = coords . shape <EOL> if len ( shape ) != <NUM_LIT:2> : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . _checkdim ( shape [ <NUM_LIT:1> ] ) <EOL> ndim = shape [ <NUM_LIT:1> ] <EOL> numpy_coords = True <EOL> cs = GEOSCoordSeq ( capi . create_cs ( ncoords , ndim ) , z = bool ( ndim == <NUM_LIT:3> ) ) <EOL> for i in range ( ncoords ) : <EOL> if numpy_coords : <EOL> cs [ i ] = coords [ i , : ] <EOL> elif isinstance ( coords [ i ] , Point ) : <EOL> cs [ i ] = coords [ i ] . tuple <EOL> else : <EOL> cs [ i ] = coords [ i ] <EOL> super ( LineString , self ) . __init__ ( self . _init_func ( cs . ptr ) , srid = srid ) <EOL> def __iter__ ( self ) : <EOL> "<STR_LIT>" <EOL> for i in range ( len ( self ) ) : <EOL> yield self [ i ] <EOL> def __len__ ( self ) : <EOL> "<STR_LIT>" <EOL> return len ( self . _cs ) <EOL> def _get_single_external ( self , index ) : <EOL> return self . _cs [ index ] <EOL> _get_single_internal = _get_single_external <EOL> def _set_list ( self , length , items ) : <EOL> ndim = self . _cs . dims <EOL> hasz = self . _cs . hasz <EOL> cs = GEOSCoordSeq ( capi . create_cs ( length , ndim ) , z = hasz ) <EOL> for i , c in enumerate ( items ) : <EOL> cs [ i ] = c <EOL> ptr = self . _init_func ( cs . ptr ) <EOL> if ptr : <EOL> capi . destroy_geom ( self . ptr ) <EOL> self . ptr = ptr <EOL> self . _post_init ( self . srid ) <EOL> else : <EOL> raise GEOSException ( '<STR_LIT>' ) <EOL> def _set_single ( self , index , value ) : <EOL> self . _checkindex ( index ) <EOL> self . _cs [ index ] = value <EOL> def _checkdim ( self , dim ) : <EOL> if dim not in ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ property <EOL> def tuple ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _cs . tuple <EOL> coords = tuple <EOL> def _listarr ( self , func ) : <EOL> """<STR_LIT>""" <EOL> lst = [ func ( i ) for i in range ( len ( self ) ) ] <EOL> if numpy : <EOL> return numpy . array ( lst ) <EOL> else : <EOL> return lst <EOL> @ property <EOL> def array ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _listarr ( self . _cs . __getitem__ ) <EOL> @ property <EOL> def x ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _listarr ( self . _cs . getX ) <EOL> @ property <EOL> def y ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _listarr ( self . _cs . getY ) <EOL> @ property <EOL> def z ( self ) : <EOL> "<STR_LIT>" <EOL> if not self . hasz : <EOL> return None <EOL> else : <EOL> return self . _listarr ( self . _cs . getZ ) <EOL> class LinearRing ( LineString ) : <EOL> _minlength = <NUM_LIT:4> <EOL> _init_func = capi . create_linearring </s>
