<s> """<STR_LIT>""" <EOL> import sys <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> range = xrange <EOL> from ctypes import c_double , cast , POINTER <EOL> from shapely . coords import required <EOL> from shapely . geos import lgeos , TopologicalError <EOL> from shapely . geometry . base import ( <EOL> BaseGeometry , geom_factory , JOIN_STYLE , geos_geom_from_py <EOL> ) <EOL> from shapely . geometry . proxy import CachingGeometryProxy <EOL> from shapely . geometry . point import Point <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class LineString ( BaseGeometry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , coordinates = None ) : <EOL> """<STR_LIT>""" <EOL> BaseGeometry . __init__ ( self ) <EOL> if coordinates is not None : <EOL> self . _set_coords ( coordinates ) <EOL> @ property <EOL> def __geo_interface__ ( self ) : <EOL> return { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : tuple ( self . coords ) <EOL> } <EOL> def svg ( self , scale_factor = <NUM_LIT:1.> , stroke_color = None ) : <EOL> """<STR_LIT>""" <EOL> if self . is_empty : <EOL> return '<STR_LIT>' <EOL> if stroke_color is None : <EOL> stroke_color = "<STR_LIT>" if self . is_valid else "<STR_LIT>" <EOL> pnt_format = "<STR_LIT:U+0020>" . join ( [ "<STR_LIT>" . format ( * c ) for c in self . coords ] ) <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( pnt_format , <NUM_LIT> * scale_factor , stroke_color ) <EOL> @ property <EOL> def ctypes ( self ) : <EOL> if not self . _ctypes_data : <EOL> self . _ctypes_data = self . coords . ctypes <EOL> return self . _ctypes_data <EOL> def array_interface ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . coords . array_interface ( ) <EOL> __array_interface__ = property ( array_interface ) <EOL> def _set_coords ( self , coordinates ) : <EOL> self . empty ( ) <EOL> self . _geom , self . _ndim = geos_linestring_from_py ( coordinates ) <EOL> coords = property ( BaseGeometry . _get_coords , _set_coords ) <EOL> @ property <EOL> def xy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . coords . xy <EOL> def parallel_offset ( <EOL> self , distance , side = '<STR_LIT:right>' , <EOL> resolution = <NUM_LIT:16> , join_style = JOIN_STYLE . round , mitre_limit = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if mitre_limit == <NUM_LIT:0.0> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> try : <EOL> return geom_factory ( self . impl [ '<STR_LIT>' ] ( <EOL> self , distance , resolution , join_style , mitre_limit , side ) ) <EOL> except OSError : <EOL> raise TopologicalError ( ) <EOL> class LineStringAdapter ( CachingGeometryProxy , LineString ) : <EOL> def __init__ ( self , context ) : <EOL> self . context = context <EOL> self . factory = geos_linestring_from_py <EOL> @ property <EOL> def _ndim ( self ) : <EOL> try : <EOL> array = self . context . __array_interface__ <EOL> n = array [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> assert n == <NUM_LIT:2> or n == <NUM_LIT:3> <EOL> return n <EOL> except AttributeError : <EOL> return len ( self . context [ <NUM_LIT:0> ] ) <EOL> @ property <EOL> def __array_interface__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . context . __array_interface__ <EOL> except AttributeError : <EOL> return self . array_interface ( ) <EOL> _get_coords = BaseGeometry . _get_coords <EOL> def _set_coords ( self , ob ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> coords = property ( _get_coords ) <EOL> def asLineString ( context ) : <EOL> """<STR_LIT>""" <EOL> return LineStringAdapter ( context ) <EOL> def geos_linestring_from_py ( ob , update_geom = None , update_ndim = <NUM_LIT:0> ) : <EOL> if isinstance ( ob , LineString ) : <EOL> if type ( ob ) == LineString : <EOL> return geos_geom_from_py ( ob ) <EOL> else : <EOL> return geos_geom_from_py ( ob , lgeos . GEOSGeom_createLineString ) <EOL> ob = required ( ob ) <EOL> try : <EOL> array = ob . __array_interface__ <EOL> assert len ( array [ '<STR_LIT>' ] ) == <NUM_LIT:2> <EOL> m = array [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if m < <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> n = array [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> except IndexError : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % str ( ob ) ) <EOL> assert n == <NUM_LIT:2> or n == <NUM_LIT:3> <EOL> if isinstance ( array [ '<STR_LIT:data>' ] , tuple ) : <EOL> cp = cast ( array [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] , POINTER ( c_double ) ) <EOL> else : <EOL> cp = array [ '<STR_LIT:data>' ] <EOL> if update_geom is not None : <EOL> cs = lgeos . GEOSGeom_getCoordSeq ( update_geom ) <EOL> if n != update_ndim : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % update_ndim ) <EOL> else : <EOL> cs = lgeos . GEOSCoordSeq_create ( m , n ) <EOL> for i in range ( m ) : <EOL> dx = c_double ( cp [ n * i ] ) <EOL> dy = c_double ( cp [ n * i + <NUM_LIT:1> ] ) <EOL> dz = None <EOL> if n == <NUM_LIT:3> : <EOL> try : <EOL> dz = c_double ( cp [ n * i + <NUM_LIT:2> ] ) <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> lgeos . GEOSCoordSeq_setX ( cs , i , dx ) <EOL> lgeos . GEOSCoordSeq_setY ( cs , i , dy ) <EOL> if n == <NUM_LIT:3> : <EOL> lgeos . GEOSCoordSeq_setZ ( cs , i , dz ) <EOL> except AttributeError : <EOL> try : <EOL> m = len ( ob ) <EOL> except TypeError : <EOL> ob = list ( ob ) <EOL> m = len ( ob ) <EOL> if m < <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> def _coords ( o ) : <EOL> if isinstance ( o , Point ) : <EOL> return o . coords [ <NUM_LIT:0> ] <EOL> else : <EOL> return o <EOL> try : <EOL> n = len ( _coords ( ob [ <NUM_LIT:0> ] ) ) <EOL> except TypeError : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % str ( ob ) ) <EOL> assert n == <NUM_LIT:2> or n == <NUM_LIT:3> <EOL> if update_geom is not None : <EOL> cs = lgeos . GEOSGeom_getCoordSeq ( update_geom ) <EOL> if n != update_ndim : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % update_ndim ) <EOL> else : <EOL> cs = lgeos . GEOSCoordSeq_create ( m , n ) <EOL> for i in range ( m ) : <EOL> coords = _coords ( ob [ i ] ) <EOL> lgeos . GEOSCoordSeq_setX ( cs , i , coords [ <NUM_LIT:0> ] ) <EOL> lgeos . GEOSCoordSeq_setY ( cs , i , coords [ <NUM_LIT:1> ] ) <EOL> if n == <NUM_LIT:3> : <EOL> try : <EOL> lgeos . GEOSCoordSeq_setZ ( cs , i , coords [ <NUM_LIT:2> ] ) <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if update_geom is not None : <EOL> return None <EOL> else : <EOL> return lgeos . GEOSGeom_createLineString ( cs ) , n <EOL> def update_linestring_from_py ( geom , ob ) : <EOL> geos_linestring_from_py ( ob , geom . _geom , geom . _ndim ) <EOL> def _test ( ) : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> _test ( ) </s>
