Example #1
0
def load_agerast(country, fromage, toage, gender, year):
    agestring = format(fromage, "02") + format(toage, "02")
    name = next((f['ISO1AL3'] for f in country))
    region = {
        'AFG': 'Asia',
        'COL': 'LAC',
        'COD': 'AFR',
        'NGA': 'AFR',
        'YEM': 'Asia'
    }[name]
    version = {'AFR': 5, 'Asia': 2, 'LAC': 1}[region]
    filename = "{region}_PPP_A{agestring}_{gender}_{year}_adj_v{version}.tif".format(
        agestring=agestring,
        gender=gender,
        year=year,
        region=region,
        version=version)
    print 'loading'
    rast = pg.RasterData('data/' + filename)
    print rast.filepath
    print 'clipping'
    clip = rast.manage.clip(country, bbox=country.bbox)
    print 'cleaning'
    del rast
    gc.collect()
    return clip
Example #2
0
def make_childrast(country, year):
    xmin, ymin, xmax, ymax = country.bbox
    calcrast = pg.RasterData(
        mode='float32',
        bbox=[xmin, ymax, xmax,
              ymin],  #xoffset=country.bbox[0], yoffset=country.bbox[3],
        xscale=0.00833333329999305,
        yscale=-0.00833333329999305)
    calcband = calcrast.add_band()
    for gend in "FM":
        print gend
        for toage in [4, 9, 14, 19]:
            fromage = toage - 4
            print fromage, toage

            rast = load_agerast(country, fromage, toage, gend, year)
            print rast.bands[0].summarystats()
            rast.view(cutoff=(0, 2))

            print 'summing'
            if toage == 19:
                calcband += rast.bands[0] * (
                    3 / 5.0
                )  # in agegroup 15-19, we only want 15-17, which is only 3 of the 5 years
            else:
                calcband += rast.bands[0]

    calcrast.bands[0] = calcband
    return calcrast
Example #3
0
    def load(self):
        # TODO: should prob be threaded
        filepath = self.filepath.get()
        data = None
        for typ in pg.vector.loader.file_extensions:
            if filepath.endswith(typ):
                data = pg.VectorData(filepath, encoding=self.encoding.get())
                break
        for typ in pg.raster.loader.file_extensions:
            if filepath.endswith(typ):
                data = pg.RasterData(filepath)
                break

        self.onsuccess(data)
Example #4
0
def viewresult(res):
    from PIL import Image
    vz = pg.renderer.Map()
    for r in res:
        props = []  #r[:-1] if len(r) > 1 else []
        if r[-1]:
            rast = r[
                -1]  # assumes last value of row is returned as geojson string
            aff = list(rast.affine)[:6]
            #aff[4] *= -1 # invert for normal images
            ims = [Image.fromarray(b.data()) for b in rast.bands]
            im = Image.merge('RGB', ims)
            d = pg.RasterData(
                image=im,
                #mode=rast.band(0).dtype, width=rast.width, height=rast.height,
                affine=aff)
            d.bands[0].nodataval = 0
            vz.add_layer(d)
    vz.render_all()
    vz.view()
Example #5
0
import pythongis as pg

rw = 6
rh = 3
w = 360
h = 180
xscale = w / float(rw)
yscale = h / float(rh)
affine = [xscale, 0, -180, 0, -yscale, 90]
r = pg.RasterData(width=rw, height=rh, mode='float32', affine=affine)
rb = r.add_band()
for y in range(rh):
    for x in range(rw):
        rb.set(x, y, (x + 1) * (y + 1))

v = pg.VectorData()
bounds = [(-180, -90), (180, -90), (180, 90), (-180, 90)]
bounds.append(bounds[0])
v.add_feature([], {'type': 'Polygon', 'coordinates': [bounds]})

m = pg.renderer.Map(2000, 1000, 'white')
m.add_layer(r)
m.add_layer(v, fillcolor=None, outlinecolor='red')
m.zoom_auto()
m.view()
Example #6
0
import pythongis as pg
import pycrs

pg.vector.data.DEFAULT_SPATIAL_INDEX = 'quadtree'

data = pg.VectorData(r"P:\(Temp Backup)\priocountries\priocountries.shp")#"C:\Users\kimok\Desktop\BIGDATA\priocountries\priocountries.shp")
rast = pg.RasterData(r"C:\Users\kimok\Documents\GitHub\AutoMap\tests\testmaps\burkina_pol96_georeferenced.tif")

#testcrs = '+proj=robin +datum=WGS84 +ellps=WGS84 +a=6378137.0 +rf=298.257223563 +pm=0 +lon_0=0 +x_0=0 +y_0=0 +units=m +axis=enu +no_defs'
#testcrs = pycrs.parse.from_sr_code(6980).to_proj4() # space
#testcrs = pycrs.parse.from_sr_code(7619).to_proj4() # goode?
#testcrs = next(pycrs.utils.search('van der grinten'))['proj4']
testcrs = next(pycrs.utils.search('eckert iv'))['proj4']




#### original crs
#data.view()

#### test on-the-fly crs
#data.view(crs=testcrs)

#### raster crs
rast.save('C:/Users/kimok/Desktop/testmap.png')
#rast.view() #testcrs)
#rast.manage.reproject(testcrs, resample='nearest').view()
#rast.manage.reproject(testcrs, resample='bilinear').view()

dsadsads
import unittest

import pythongis as pg

# data

pointdata = pg.VectorData('data/ne_10m_populated_places_simple.shp',
                          encoding='latin')
linedata = pg.VectorData('data/ne_10m_railroads.shp', encoding='latin')
polygondata = pg.VectorData('data/ne_10m_admin_0_countries.shp',
                            encoding='latin')
rasterdata = pg.RasterData('data/land_shallow_topo_2048.png')
rasterdata.set_geotransform(width=2048,
                            height=1024,
                            affine=[0.175781250, 0, -180, 0, -0.175781250, 90])

# base class


class BaseTestCases:
    class DrawShapes(unittest.TestCase):
        width = 600
        height = 300
        kwargs = {'fillcolor': 'yellow', 'outlinecolor': 'black'}
        output_prefix = 'render_projections'
        crs = None

        def create_map(self):
            self.map = pg.renderer.Map(self.width,
                                       self.height,
                                       background='gray',
Example #8
0
import pythongis as pg
import gc

countries = pg.VectorData(r"C:\Users\kimok\Desktop\ch est\data\cshapes.shp")
countries = countries.select(
    lambda f: f["GWCODE"] != -1 and f["GWEYEAR"] == 2016)
print countries

# mapit
#rast = pg.RasterData(r'C:\Users\kimok\Downloads\F182013.v4c_web.stable_lights.avg_vis.tif')
rast = pg.RasterData(
    r'C:\Users\kimok\Downloads\SVDNB_npp_20170701-20170731_75N060W_vcmcfg_v10_c201708061230.avg_rade9.tif'
)
print rast

for iso in ['NGA', 'COD', 'YEM']:
    print iso
    c = countries.select(lambda f: f["ISO1AL3"] == iso)
    clip = rast.manage.clip(c, bbox=c.bbox)
    print clip
    clip.view(cutoff=(0.1, 99.9))

print 'finished!'
import pythongis as pg

###
print("\n" + "raster test")
rast = pg.RasterData(
    r"C:\Users\kimo\Dropbox\Work\Workplace\Geobook15\pygeo book 2\code\(raw sandbox,incl abondoned ideas)\test_files\geotiff\TrueMarble.16km.2700x1350.tif"
)
for b in rast.bands:
    #b.img.show()
    b.nodataval = 0  # note: if set nodataval first and then manually override mask, means previous nodatavals wont get affected
    #b.mask.show()#view(1000,500)
    #b.img.show()
#rast.bands.pop(-1)
#rast.bands = rast.bands[:1]
print rast.bands
#rast.mask.show()
#rast.mask = rast.bands[0].conditional("val == 0").img
#rast.mask.show()
#rast.view(1000,500, type="colorscale", gradcolors=[(0,0,255),(0,255,0),(255,0,0)])
#iyiy

###
print("\n" + "multimap test")
mapp = pg.renderer.Map()
vect = pg.vector.data.VectorData(
    r"C:\Users\kimo\Dropbox\Work\Workplace\Geobook15\pygeo book 2\code\(raw sandbox,incl abondoned ideas)\test_files\shp\domestic.shp"
)
print vect.fields

mapp.add_layer(rast,
               bandnum=1,
Example #10
0
import pythongis as pg
import gc

countries = pg.VectorData("data/ne_10m_admin_0_countries.shp")
print countries

# mapit
rast = pg.RasterData(r'C:\Users\kimok\Downloads\F182013.v4c_web.stable_lights.avg_vis.tif')

for iso in ['TUR']:
    print iso
    c = countries.select(lambda f: f["ISO_A3"]==iso)
    clip = rast.manage.clip(c, bbox=c.bbox)
    print clip

    mapp = pg.renderer.Map()
    mapp.add_layer(clip)
    mapp.add_layer(c, fillcolor=None)
    mapp.add_legend()
    mapp.view()

    c = c.manage.reproject('+proj=robin')
    clip = clip.manage.reproject('+proj=robin')

    mapp = pg.renderer.Map()
    mapp.add_layer(clip)
    mapp.add_layer(c, fillcolor=None)
    mapp.add_legend()
    mapp.view()
Example #11
0
import pythongis as pg
import gc

# create the test data
##focus = pg.VectorData("data/ne_10m_admin_0_countries.shp", select=lambda f: f['ISO_A3']=='IRN')
##print focus
##for yr in [1990,2000,2015]:
##    rast = pg.RasterData(r"C:\Users\kimok\Desktop\redd barna\Input\pop%s.tif"%yr)
##    clip = rast.manage.clip(focus, bbox=focus.bbox)
##    print clip
##    clip.save('data/pop%s.tif'%yr)

for yr, rast in pg.raster.manager.sequence(
        range(1990, 2015 + 1),
        rasts={
            1990: lambda: pg.RasterData(r'data/pop1990.tif'),
            2000: lambda: pg.RasterData(r'data/pop2000.tif'),
            2015: lambda: pg.RasterData(r'data/pop2015.tif')
        }):
    print yr
    #print rast.bands[0].summarystats()
    #rast.view()
Example #12
0
    def view_image(self, mapID):
        import pythongis as pg
        import urllib
        import io

        # get map info
        link, width, height, mapregion = self.get(
            'select link,width,height,mapregion from maps where oid=?',
            (mapID, ))

        # init renderer
        render = pg.renderer.Map(width, height)
        render._create_drawer()
        render.drawer.pixel_space()

        # load image
        print 'loading', link
        if link.startswith('http'):
            fobj = io.BytesIO(urllib.urlopen(link).read())
            img = PIL.Image.open(fobj)
        else:
            img = PIL.Image.open(link)
        rast = pg.RasterData(image=img)
        render.add_layer(rast)

        # add image regions
        bounds = pg.VectorData()
        bounds.add_feature([], json.loads(mapregion))
        render.add_layer(bounds,
                         fillcolor=None,
                         outlinecolor='red',
                         outlinewidth=0.2)

        # add text
        texts = pg.VectorData(fields=['text', 'conf'])
        textquery = self.query(
            'select text,conf,left,top,width,height from maptext where maptext.map = ?',
            (mapID, ))
        for text in textquery:
            x1, y1 = text['left'], text['top']
            x2, y2 = x1 + text['width'], y1 + text['height']
            box = [x1, y1, x2, y2]
            row = [text['text'], text['conf']]
            geoj = {
                'type': 'Polygon',
                'coordinates': [[(x1, y1), (x2, y1), (x2, y2), (x1, y2)]]
            }
            texts.add_feature(row, geoj)
        render.add_layer(texts,
                         fillcolor=None,
                         outlinecolor='green',
                         outlinewidth=0.5)

        # add tiepoints
        tiepoints = pg.VectorData()
        tiepointquery = self.query(
            'select col,row from maptiepoints where maptiepoints.map = ?',
            (mapID, ))
        for col, row in tiepointquery:
            geoj = {'type': 'Point', 'coordinates': (col, row)}
            tiepoints.add_feature([], geoj)
        if len(tiepoints):
            render.add_layer(tiepoints,
                             fillcolor=None,
                             outlinecolor='red',
                             outlinewidth=0.3)

        # view
        render.zoom_auto()
        render.view()
Example #13
0
        #print lat,lon
        d.add_feature(row=[col, row, i],
                      geometry={
                          'type': 'Point',
                          'coordinates': (lon, lat)
                      })

d.view(fillcolor=dict(breaks='proportional', key='i'))
dvec = d

# raw grid
data = [v for r in vals[time] for v in r]
import PIL, PIL.Image
img = PIL.Image.new('L', (88, 88))
img.putdata(data)
d = pg.RasterData(image=img, width=88, height=88, bbox=[0, 0, 1, 1])
d.view()

# projected stereo
import pyproj
stereo = pyproj.Proj(init='EPSG:3411')
d = pg.VectorData(fields=['col', 'row', 'i', 'val'])
for col in range(88):
    #print col
    for row in range(88):
        i = col * row + row
        lat, lon = lats[row, col], lons[row, col]
        val = vals[time, row, col]
        if val == 0: continue
        #print lat,lon
        x, y = stereo(lon, lat)