Exemplo n.º 1
0
def get_wdb_boundaries(resolution,level,rivers=False):
    polymeta = []; polybounds = []
    if rivers:
        filename = 'WDBII_shp/%s/WDBII_river_%s_L%02i' % (resolution, resolution, level)
    else:
        filename = 'WDBII_shp/%s/WDBII_border_%s_L%s' % (resolution, resolution, level)
    print filename
    shf = Reader(filename)
    fields = shf.fields
    for shprec in shf.shapeRecords():
        shp = shprec.shape; rec = shprec.record
        parts = shp.parts.tolist()
        if parts != [0]: 
            print 'multipart polygon'
            raise SystemExit
        verts = shp.points
        lons, lats = list(zip(*verts))
        north = max(lats); south = min(lats)
        attdict={}
        for r,key in zip(rec,fields[1:]):
            attdict[key[0]]=r
        area = -1
        id = attdict['id']
        polymeta.append([-1,-1,south,north,len(lons),id])
        b = np.empty((len(lons),2),np.float32)
        b[:,0] = lons; b[:,1] = lats
        if lsd is not None:
            b = quantize(b,lsd)
        polybounds.append(b)
    return polybounds, polymeta
Exemplo n.º 2
0
def get_coast_polygons(resolution):
    polymeta = []; polybounds = []
    for level in [1,2,3,4]:
        filename = 'GSHHS_shp/%s/GSHHS_%s_L%s' % (resolution, resolution, level)
        #filename = 'WDBII_shp/%s/WDBII_border_%s_L%s' % (resolution, resolution, level)
        print filename
        shf = Reader(filename)
        fields = shf.fields
        try:
            shf.shapeRecords()
        except:
            continue
        for shprec in shf.shapeRecords():
            shp = shprec.shape; rec = shprec.record
            parts = shp.parts.tolist()
            if parts != [0]: 
                print 'multipart polygon'
                raise SystemExit
            verts = shp.points
            lons, lats = list(zip(*verts))
            north = max(lats); south = min(lats)
            attdict={}
            for r,key in zip(rec,fields[1:]):
                attdict[key[0]]=r
            area = attdict['area']
            id = attdict['id']
            polymeta.append([level,area,south,north,len(lons),id])
            b = np.empty((len(lons),2),np.float32)
            b[:,0] = lons; b[:,1] = lats
            if lsd is not None:
                b = quantize(b,lsd)
            polybounds.append(b)
    return polybounds, polymeta
Exemplo n.º 3
0
def shapefile2data(shapefile, countries=False):
    shf = ShapeReader(shapefile)
    fields = [f[0] for f in shf.fields if isinstance(f, list)]
    latlons = None
    if countries and 'LAT' not in fields or 'LON' not in fields:
        with open(join(dirname(abspath(__file__)), 'data', 'latlons.json')) as fh:
            latlons = json.load(fh)
    records = []
    iso2s = set()
    for record in shf.shapeRecords():
        datadict = dict(zip(fields, record.record))
        if countries:
            iso2 = datadict['ISO_A2']
            iso2s.add(iso2)
            if latlons is not None and iso2 in latlons:
                lat, lon = latlons[iso2]
                datadict['LAT'] = lat
                datadict['LON'] = lon
        datadict['SHAPE'] = [record.shape]
        records.append(datadict)
    if countries:
        for iso2, latlon in latlons.items():
            if iso2 in iso2s:
                continue
            lat, lon = latlon
            datadict = { 'ISO_A2': iso2, 'LAT': lat, 'LON': lon }
            records.append(datadict)
    return records
Exemplo n.º 4
0
def get_coast_polygons(resolution):
    polymeta = []
    polybounds = []
    for level in [1, 2, 3, 4]:
        filename = 'GSHHS_shp/%s/GSHHS_%s_L%s' % (resolution, resolution,
                                                  level)
        #filename = 'WDBII_shp/%s/WDBII_border_%s_L%s' % (resolution, resolution, level)
        print filename
        shf = Reader(filename)
        fields = shf.fields
        try:
            shf.shapeRecords()
        except:
            continue
        for shprec in shf.shapeRecords():
            shp = shprec.shape
            rec = shprec.record
            parts = shp.parts.tolist()
            if parts != [0]:
                print 'multipart polygon'
                raise SystemExit
            verts = shp.points
            lons, lats = list(zip(*verts))
            north = max(lats)
            south = min(lats)
            attdict = {}
            for r, key in zip(rec, fields[1:]):
                attdict[key[0]] = r
            area = attdict['area']
            id = attdict['id']
            polymeta.append([level, area, south, north, len(lons), id])
            b = np.empty((len(lons), 2), np.float32)
            b[:, 0] = lons
            b[:, 1] = lats
            if lsd is not None:
                b = quantize(b, lsd)
            polybounds.append(b)
    return polybounds, polymeta
Exemplo n.º 5
0
def get_wdb_boundaries(resolution, level, rivers=False):
    polymeta = []
    polybounds = []
    if rivers:
        filename = 'WDBII_shp/%s/WDBII_river_%s_L%02i' % (resolution,
                                                          resolution, level)
    else:
        filename = 'WDBII_shp/%s/WDBII_border_%s_L%s' % (resolution,
                                                         resolution, level)
    print filename
    shf = Reader(filename)
    fields = shf.fields
    for shprec in shf.shapeRecords():
        shp = shprec.shape
        rec = shprec.record
        parts = shp.parts.tolist()
        if parts != [0]:
            print 'multipart polygon'
            raise SystemExit
        verts = shp.points
        lons, lats = list(zip(*verts))
        north = max(lats)
        south = min(lats)
        attdict = {}
        for r, key in zip(rec, fields[1:]):
            attdict[key[0]] = r
        area = -1
        id = attdict['id']
        polymeta.append([-1, -1, south, north, len(lons), id])
        b = np.empty((len(lons), 2), np.float32)
        b[:, 0] = lons
        b[:, 1] = lats
        if lsd is not None:
            b = quantize(b, lsd)
        polybounds.append(b)
    return polybounds, polymeta