<s> import numpy as np <EOL> try : <EOL> from matplotlib import pyplot as pb <EOL> from matplotlib . patches import Polygon <EOL> from matplotlib . collections import PatchCollection <EOL> try : <EOL> __IPYTHON__ <EOL> pb . ion ( ) <EOL> except NameError : <EOL> pass <EOL> except : <EOL> pass <EOL> import re <EOL> def plot ( shape_records , facecolor = '<STR_LIT:w>' , edgecolor = '<STR_LIT:k>' , linewidths = <NUM_LIT> , ax = None , xlims = None , ylims = None ) : <EOL> """<STR_LIT>""" <EOL> if ax is None : <EOL> fig = pb . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> for srec in shape_records : <EOL> points = np . vstack ( srec . shape . points ) <EOL> sparts = srec . shape . parts <EOL> par = list ( sparts ) + [ points . shape [ <NUM_LIT:0> ] ] <EOL> polygs = [ ] <EOL> for pj in range ( len ( sparts ) ) : <EOL> polygs . append ( Polygon ( points [ par [ pj ] : par [ pj + <NUM_LIT:1> ] ] ) ) <EOL> ax . add_collection ( PatchCollection ( polygs , facecolor = facecolor , edgecolor = edgecolor , linewidths = linewidths ) ) <EOL> _box = np . vstack ( [ srec . shape . bbox for srec in shape_records ] ) <EOL> minx , miny = np . min ( _box [ : , : <NUM_LIT:2> ] , <NUM_LIT:0> ) <EOL> maxx , maxy = np . max ( _box [ : , <NUM_LIT:2> : ] , <NUM_LIT:0> ) <EOL> if xlims is not None : <EOL> minx , maxx = xlims <EOL> if ylims is not None : <EOL> miny , maxy = ylims <EOL> ax . set_xlim ( minx , maxx ) <EOL> ax . set_ylim ( miny , maxy ) <EOL> def string_match ( sf , regex , field = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> index = [ ] <EOL> shape_records = [ ] <EOL> for rec in enumerate ( sf . shapeRecords ( ) ) : <EOL> m = re . search ( regex , rec [ <NUM_LIT:1> ] . record [ field ] ) <EOL> if m is not None : <EOL> index . append ( rec [ <NUM_LIT:0> ] ) <EOL> shape_records . append ( rec [ <NUM_LIT:1> ] ) <EOL> return index , shape_records <EOL> def bbox_match ( sf , bbox , inside_only = True ) : <EOL> """<STR_LIT>""" <EOL> A , B , C , D = bbox <EOL> index = [ ] <EOL> shape_records = [ ] <EOL> for rec in enumerate ( sf . shapeRecords ( ) ) : <EOL> a , b , c , d = rec [ <NUM_LIT:1> ] . shape . bbox <EOL> if inside_only : <EOL> if A <= a and B <= b and C >= c and D >= d : <EOL> index . append ( rec [ <NUM_LIT:0> ] ) <EOL> shape_records . append ( rec [ <NUM_LIT:1> ] ) <EOL> else : <EOL> cond1 = A <= a and B <= b and C >= a and D >= b <EOL> cond2 = A <= c and B <= d and C >= c and D >= d <EOL> cond3 = A <= a and D >= d and C >= a and B <= d <EOL> cond4 = A <= c and D >= b and C >= c and B <= b <EOL> cond5 = a <= C and b <= B and d >= D <EOL> cond6 = c <= A and b <= B and d >= D <EOL> cond7 = d <= B and a <= A and c >= C <EOL> cond8 = b <= D and a <= A and c >= C <EOL> if cond1 or cond2 or cond3 or cond4 or cond5 or cond6 or cond7 or cond8 : <EOL> index . append ( rec [ <NUM_LIT:0> ] ) <EOL> shape_records . append ( rec [ <NUM_LIT:1> ] ) <EOL> return index , shape_records <EOL> def plot_bbox ( sf , bbox , inside_only = True ) : <EOL> """<STR_LIT>""" <EOL> index , shape_records = bbox_match ( sf , bbox , inside_only ) <EOL> A , B , C , D = bbox <EOL> plot ( shape_records , xlims = [ bbox [ <NUM_LIT:0> ] , bbox [ <NUM_LIT:2> ] ] , ylims = [ bbox [ <NUM_LIT:1> ] , bbox [ <NUM_LIT:3> ] ] ) <EOL> def plot_string_match ( sf , regex , field , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> index , shape_records = string_match ( sf , regex , field ) <EOL> plot ( shape_records , ** kwargs ) <EOL> def new_shape_string ( sf , name , regex , field = <NUM_LIT:2> , type = None ) : <EOL> import shapefile <EOL> if type is None : <EOL> type = shapefile . POINT <EOL> newshp = shapefile . Writer ( shapeType = sf . shapeType ) <EOL> newshp . autoBalance = <NUM_LIT:1> <EOL> index , shape_records = string_match ( sf , regex , field ) <EOL> _fi = [ sf . fields [ j ] for j in index ] <EOL> for f in _fi : <EOL> newshp . field ( name = f [ <NUM_LIT:0> ] , fieldType = f [ <NUM_LIT:1> ] , size = f [ <NUM_LIT:2> ] , decimal = f [ <NUM_LIT:3> ] ) <EOL> _shre = shape_records <EOL> for sr in _shre : <EOL> _points = [ ] <EOL> _parts = [ ] <EOL> for point in sr . shape . points : <EOL> _points . append ( point ) <EOL> _parts . append ( _points ) <EOL> newshp . line ( parts = _parts ) <EOL> newshp . records . append ( sr . record ) <EOL> print ( len ( sr . record ) ) <EOL> newshp . save ( name ) <EOL> print ( index ) <EOL> def apply_bbox ( sf , ax ) : <EOL> """<STR_LIT>""" <EOL> limits = sf . bbox <EOL> xlim = limits [ <NUM_LIT:0> ] , limits [ <NUM_LIT:2> ] <EOL> ylim = limits [ <NUM_LIT:1> ] , limits [ <NUM_LIT:3> ] <EOL> ax . set_xlim ( xlim ) <EOL> ax . set_ylim ( ylim ) </s>
