Note

This page was generated from examples/notebooks/instantiation.ipynb.

Instantiation#

[1]:
import geopandas
import pandas
import starepandas
import pystare
import shapely
import matplotlib.pyplot as plt

Points#

Creating a naive dataframe#

[2]:
cities = ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas']
countries = ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela']
latitudes = [-34.58, -15.78, -33.45, 4.60, 10.48]
longitudes = [-58.66, -47.91, -70.66, -74.08, -66.86]
data =  {'City': cities,  'Country': countries,
         'Latitude': latitudes, 'Longitude': longitudes}

df = pandas.DataFrame(data)

Creating a geodataframe#

[3]:
geom = geopandas.points_from_xy(df.Longitude, df.Latitude)
gdf = geopandas.GeoDataFrame(df, geometry=geom)
gdf
[3]:
City Country Latitude Longitude geometry
0 Buenos Aires Argentina -34.58 -58.66 POINT (-58.66000 -34.58000)
1 Brasilia Brazil -15.78 -47.91 POINT (-47.91000 -15.78000)
2 Santiago Chile -33.45 -70.66 POINT (-70.66000 -33.45000)
3 Bogota Colombia 4.60 -74.08 POINT (-74.08000 4.60000)
4 Caracas Venezuela 10.48 -66.86 POINT (-66.86000 10.48000)

Creating sids#

[4]:
# Those are all identical
sids = starepandas.sids_from_xy(df.Longitude, df.Latitude, level=5)
sids = starepandas.sids_from_xy_df(gdf, num_workers=2, level=5)
#sids = starepandas.sids_from_gdf(gdf, level=5)

sdf = starepandas.STAREDataFrame(gdf)
sdf.set_sids(sids, inplace=True)
[5]:
sdf
[5]:
City Country Latitude Longitude geometry tids
0 Buenos Aires Argentina -34.58 -58.66 POINT (-58.66000 -34.58000) 2663379193440875365
1 Brasilia Brazil -15.78 -47.91 POINT (-47.91000 -15.78000) 2867415364672350629
2 Santiago Chile -33.45 -70.66 POINT (-70.66000 -33.45000) 2723774768829278533
3 Bogota Colombia 4.60 -74.08 POINT (-74.08000 4.60000) 2667981979956219493
4 Caracas Venezuela 10.48 -66.86 POINT (-66.86000 10.48000) 2494081632617553381
[5]:
trixels = sdf.make_trixels()
sdf.set_trixels(trixels, inplace=True)
[6]:
sdf.sids
[6]:
0    2663379193440875365
1    2867415364672350629
2    2723774768829278533
3    2667981979956219493
4    2494081632617553381
Name: sids, dtype: int64
[7]:
sdf.geometry
[7]:
0    POINT (-58.66000 -34.58000)
1    POINT (-47.91000 -15.78000)
2    POINT (-70.66000 -33.45000)
3      POINT (-74.08000 4.60000)
4     POINT (-66.86000 10.48000)
Name: geometry, dtype: geometry
[8]:
sdf
[8]:
City Country Latitude Longitude geometry sids trixels
0 Buenos Aires Argentina -34.58 -58.66 POINT (-58.66000 -34.58000) 2663379193440875365 POLYGON ((-59.87026 -34.71551, -56.40359 -35.1...
1 Brasilia Brazil -15.78 -47.91 POINT (-47.91000 -15.78000) 2867415364672350629 POLYGON ((-49.47476 -12.22984, -51.65012 -15.8...
2 Santiago Chile -33.45 -70.66 POINT (-70.66000 -33.45000) 2723774768829278533 POLYGON ((-71.62565 -35.43810, -69.94333 -32.7...
3 Bogota Colombia 4.60 -74.08 POINT (-74.08000 4.60000) 2667981979956219493 POLYGON ((-75.23698 2.57332, -73.80858 5.00265...
4 Caracas Venezuela 10.48 -66.86 POINT (-66.86000 10.48000) 2494081632617553381 POLYGON ((-67.79536 8.98813, -66.27210 11.3867...

Some plotting#

[9]:
fig, ax = plt.subplots()
ax.grid(True)

sdf.plot(ax=ax)
sdf.plot(ax=ax, trixels=True)
sdf.plot(ax=ax, trixels=False, color='r')

sdf.plot(ax=ax, column='City')
[9]:
<AxesSubplot:>
../../_images/examples_notebooks_instantiation_15_1.png

From granule#

Bootstrap#

[10]:
fname = '../tests/data/granules/MOD05_L2.A2019336.0000.061.2019336211522.hdf'
modis = starepandas.read_granule(fname,
                                 add_sids=True,
                                 adapt_resolution=True)

Use Sidecar#

[11]:
modis = starepandas.read_granule(fname, sidecar=True)
modis
[11]:
sids Scan_Start_Time Solar_Zenith Solar_Azimuth Sensor_Zenith Sensor_Azimuth Water_Vapor_Infrared
0 4298473764500464809 8.493984e+08 147.089997 -22.959999 65.339999 -89.519998 NaN
1 4298458168380511209 8.493984e+08 146.999997 -23.539999 64.719999 -89.799998 NaN
2 4297394569014717897 8.493984e+08 146.919997 -24.099999 64.109999 -90.079998 NaN
3 4297300698872999369 8.493984e+08 146.839997 -24.639999 63.509999 -90.339998 NaN
4 4297290857922121161 8.493984e+08 146.759997 -25.159999 62.909999 -90.589998 NaN
... ... ... ... ... ... ... ...
109615 3727686638370615689 8.493987e+08 121.569997 -74.679998 62.229999 47.999999 NaN
109616 3727661374545790857 8.493987e+08 121.469997 -75.049998 62.819999 47.719999 NaN
109617 3727838256925064969 8.493987e+08 121.369997 -75.439998 63.419999 47.429999 NaN
109618 3727843063731949801 8.493987e+08 121.259997 -75.829998 64.019999 47.139999 NaN
109619 3727853163225616425 8.493987e+08 121.149997 -76.249998 64.639999 46.819999 NaN

109620 rows × 7 columns

Polygons#

US State#

[12]:
us = geopandas.read_file('../tests/data/tl_2017_us_state.gpkg')
us = us.to_crs('EPSG:4326')
us['geom_simple'] = us.simplify(0.002)
us.set_geometry('geom_simple', inplace=True)
[13]:
stare = starepandas.sids_from_gdf(us, level=7, force_ccw=True)
us = starepandas.STAREDataFrame(us, sids=stare)
us.add_trixels(inplace=True)
us._geometry_column_name
[13]:
'geom_simple'
[14]:
fig, ax = plt.subplots()
ax.grid(True)

state = us[us.NAME=='California']
state.plot(ax=ax, trixels=True, boundary=True, color='y', zorder=0)
state.plot(ax=ax, trixels=False, facecolor="none", edgecolor='blue', zorder=1)
[14]:
<AxesSubplot:>
../../_images/examples_notebooks_instantiation_25_1.png

Africa#

[15]:
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.sort_values(by='name', inplace=True)
africa = world[world.continent=='Africa']
[16]:
stare = starepandas.sids_from_gdf(africa, level=7, force_ccw=True)
africa = starepandas.STAREDataFrame(africa, sids=stare)
trixels = africa.make_trixels()
africa.set_trixels(trixels, inplace=True)
[17]:
africa
[17]:
pop_est continent name iso_a3 gdp_md_est geometry sids trixels
82 40969443 Africa Algeria DZA 609400.0 POLYGON ((-8.68440 27.39574, -8.66512 27.58948... [4485585228861014020, 4490651778441805829, 449... MULTIPOLYGON (((0.00000 35.26439, -4.26668 28....
74 29310273 Africa Angola AGO 189000.0 MULTIPOLYGON (((12.99552 -4.78110, 12.63161 -4... [4464228315003092999, 4464263499375181831, 446... MULTIPOLYGON (((12.02286 -4.04284, 12.42307 -4...
54 11038805 Africa Benin BEN 24310.0 POLYGON ((2.69170 6.25882, 1.86524 6.14216, 1.... [4415849803380948999, 4416096093985570823, 441... MULTIPOLYGON (((2.28947 6.63805, 1.35273 6.753...
49 2214858 Africa Botswana BWA 35900.0 POLYGON ((29.43219 -22.09131, 28.01724 -22.827... [21392098230009861, 21955048183431173, 2589569... MULTIPOLYGON (((24.29559 -19.47089, 22.03163 -...
65 20107509 Africa Burkina Faso BFA 32990.0 POLYGON ((-5.40434 10.37074, -5.47056 10.95127... [4500503402626678790, 4500644140115034118, 450... MULTIPOLYGON (((0.33681 14.05776, -1.65941 14....
75 11466756 Africa Burundi BDI 7892.0 POLYGON ((30.46967 -2.41385, 30.52766 -2.80762... [4393402173988274183, 4393437358360363015, 439... MULTIPOLYGON (((29.82200 -2.10228, 30.24394 -2...
57 24994885 Africa Cameroon CMR 77240.0 POLYGON ((14.49579 12.85940, 14.89336 12.21905... [4413668372311441414, 4413809109799796742, 441... MULTIPOLYGON (((11.63286 5.39078, 12.33798 4.1...
66 5625118 Africa Central African Rep. CAF 3206.0 POLYGON ((27.37423 5.23394, 27.04407 5.12785, ... [4368491638549381126, 4521614025879977990, 452... MULTIPOLYGON (((15.35858 4.85021, 16.17868 3.5...
15 12075985 Africa Chad TCD 30590.0 POLYGON ((23.83766 19.58047, 23.88689 15.61084... [4594234569871327237, 4596486369685012485, 460... MULTIPOLYGON (((20.83798 11.25168, 16.43269 11...
67 4954674 Africa Congo COG 30270.0 POLYGON ((18.45307 3.50439, 18.39379 2.90044, ... [4432949408216121350, 4433230883192832006, 443... MULTIPOLYGON (((14.68006 0.27864, 16.65430 -0....
60 24184810 Africa Côte d'Ivoire CIV 87120.0 POLYGON ((-8.02994 10.20653, -7.89959 10.29738... [4400579785894395909, 4402831585708081158, 447... MULTIPOLYGON (((-7.09785 7.70991, -5.69224 5.2...
11 83301151 Africa Dem. Rep. Congo COD 66010.0 POLYGON ((29.34000 -4.49998, 29.51999 -5.41998... [4370743438363066372, 4372995238176751620, 437... MULTIPOLYGON (((21.90167 -5.46143, 26.06155 -1...
166 865267 Africa Djibouti DJI 3345.0 POLYGON ((42.35156 12.54223, 42.77964 12.45542... [4111927197277618183, 4111997566021795847, 411... MULTIPOLYGON (((43.07350 11.08592, 43.57883 11...
163 97041072 Africa Egypt EGY 1105000.0 POLYGON ((36.86623 22.00000, 32.90000 22.00000... [4582975570802900997, 4584101470709743621, 451... MULTIPOLYGON (((32.69945 23.66892, 31.46284 27...
69 778358 Africa Eq. Guinea GNQ 31770.0 POLYGON ((9.64916 2.28387, 11.27645 2.26105, 1... [4414653534729928711, 4414688719102017543, 441... MULTIPOLYGON (((10.73117 0.86207, 11.30815 1.3...
154 5918919 Africa Eritrea ERI 9169.0 POLYGON ((36.42951 14.42211, 36.32322 14.82249... [4111997566021795847, 4505851427184181255, 450... MULTIPOLYGON (((42.74205 12.93268, 42.90834 12...
165 105350020 Africa Ethiopia ETH 174700.0 POLYGON ((47.78942 8.00300, 44.96360 5.00162, ... [4506414377137602565, 4510355026811551749, 451... MULTIPOLYGON (((36.20602 8.69219, 39.97138 8.0...
68 1772255 Africa Gabon GAB 35980.0 POLYGON ((11.27645 2.26105, 11.75167 2.32676, ... [4432104983285989382, 4434919733053095942, 443... MULTIPOLYGON (((10.73117 0.86207, 12.70572 0.5...
80 2051363 Africa Gambia GMB 3387.0 POLYGON ((-16.71373 13.59496, -15.62460 13.623... [4216072938660560903, 4216108123032649735, 421... MULTIPOLYGON (((-14.51077 14.26876, -14.14497 ...
59 27499924 Africa Ghana GHA 120800.0 POLYGON ((0.02380 11.01868, -0.04978 10.70692,... [4400016835940974598, 4400298310917685254, 440... MULTIPOLYGON (((0.41556 6.86747, -1.46014 7.08...
61 12413867 Africa Guinea GIN 16080.0 POLYGON ((-13.70048 12.58618, -13.21782 12.575... [4215509988707139590, 4469259680211795974, 421... MULTIPOLYGON (((-13.05851 11.83327, -14.57478 ...
62 1792338 Africa Guinea-Bissau GNB 2851.0 POLYGON ((-16.67745 12.38485, -16.14772 12.547... [4215721094939672583, 4216530335497715719, 421... MULTIPOLYGON (((-14.57478 11.34297, -15.33094 ...
13 47615739 Africa Kenya KEN 152700.0 POLYGON ((39.20222 -4.67677, 37.76690 -3.67712... [4367365738642538501, 4360047389248061446, 436... MULTIPOLYGON (((37.60681 1.26475, 35.59700 -0....
26 1958042 Africa Lesotho LSO 6019.0 POLYGON ((28.97826 -28.95560, 29.32517 -29.257... [22553182508941319, 22588366881030151, 2262355... MULTIPOLYGON (((28.67505 -29.43723, 28.07904 -...
63 4689021 Africa Liberia LBR 3881.0 POLYGON ((-8.43930 7.68604, -8.48545 7.39521, ... [4396216923755380743, 4396674320592535559, 439... MULTIPOLYGON (((-9.45410 5.63922, -10.39260 5....
164 6653210 Africa Libya LBY 90890.0 POLYGON ((25.00000 22.00000, 25.00000 20.00304... [4590293920197378053, 4607745368753438725, 460... MULTIPOLYGON (((14.03624 27.22563, 17.09002 30...
78 25054161 Africa Madagascar MDG 36860.0 POLYGON ((49.54352 -12.46983, 49.80898 -12.895... [92323792361095173, 94012642221359109, 9851624... MULTIPOLYGON (((47.59434 -14.77902, 45.53750 -...
71 19196246 Africa Malawi MWI 21200.0 POLYGON ((32.75938 -9.23060, 33.73972 -9.41715... [82190693199511559, 82261061943689223, 8229624... MULTIPOLYGON (((32.89359 -11.84485, 32.37360 -...
52 17885245 Africa Mali MLI 38090.0 POLYGON ((-11.51394 12.44299, -11.46790 12.754... [4474326229792587781, 4497970127836282885, 450... MULTIPOLYGON (((-3.65931 14.47308, -7.54192 14...
53 3758571 Africa Mauritania MRT 16710.0 POLYGON ((-17.06342 20.99975, -16.84519 21.333... [4217621051032469509, 4218184000985890821, 421... MULTIPOLYGON (((-8.02332 22.00360, -11.22501 2...
162 33986655 Africa Morocco MAR 282800.0 POLYGON ((-2.16991 35.16840, -1.79299 34.52792... [4224939400426946565, 4232257749821423621, 422... MULTIPOLYGON (((-7.22640 33.89483, -5.70567 31...
72 26573706 Africa Mozambique MOZ 35010.0 POLYGON ((34.55999 -11.52002, 35.31240 -11.439... [86694292826882053, 88946092640567301, 1041457... MULTIPOLYGON (((37.99192 -15.46073, 37.48603 -...
50 2484780 Africa Namibia NAM 25990.0 POLYGON ((19.89577 -24.76779, 19.89473 -28.461... [4503599627370501, 4325707442089361413, 433809... MULTIPOLYGON (((19.71050 -23.13179, 17.32631 -...
55 19245344 Africa Niger NER 20150.0 POLYGON ((14.85130 22.86295, 15.09689 21.30852... [4478829829419958277, 4598175219545276421, 447... MULTIPOLYGON (((8.44597 20.22250, 6.34522 16.9...
56 190632261 Africa Nigeria NGA 1089000.0 POLYGON ((2.69170 6.25882, 2.74906 7.87073, 2.... [4483333429047328772, 4481644579187064837, 441... MULTIPOLYGON (((4.31733 13.57705, 7.90064 5.90...
169 11901484 Africa Rwanda RWA 21970.0 POLYGON ((30.41910 -1.13466, 30.81613 -1.69891... [4391713324128010247, 4392417011569786887, 439... MULTIPOLYGON (((29.82200 -2.10228, 29.39958 -1...
176 13026129 Africa S. Sudan SSD 20880.0 POLYGON ((30.83385 3.50917, 29.95350 4.17370, ... [4534561874808668165, 4524710250623795206, 452... MULTIPOLYGON (((32.32467 9.38401, 29.32381 6.2...
51 14668522 Africa Senegal SEN 39720.0 POLYGON ((-16.71373 13.59496, -17.12611 14.373... [4216354413637271558, 4222828338101616646, 422... MULTIPOLYGON (((-13.78106 13.05203, -12.25420 ...
64 6163195 Africa Sierra Leone SLE 10640.0 POLYGON ((-13.24655 8.90305, -12.71196 9.34271... [4467711567839887366, 4395583605057781767, 439... MULTIPOLYGON (((-10.86501 8.08275, -11.95546 9...
12 7531386 Africa Somalia SOM 4719.0 POLYGON ((41.58513 -1.68325, 40.99300 -0.85829... [4107564335138603014, 4107986547603668998, 410... MULTIPOLYGON (((44.36499 3.69592, 44.68282 1.8...
167 3500000 Africa Somaliland -99 12250.0 POLYGON ((48.94820 11.41062, 48.94820 11.41062... [4114178997091303430, 4114460472068014086, 411... MULTIPOLYGON (((45.73552 9.40224, 47.06182 8.5...
25 54841552 Africa South Africa ZAF 739100.0 POLYGON ((16.34498 -28.57671, 16.82402 -28.082... [2814749767106566, 3096224743817223, 313140911... MULTIPOLYGON (((18.95439 -31.03247, 17.67298 -...
14 37345935 Africa Sudan SDN 176300.0 POLYGON ((24.56737 8.22919, 23.80581 8.66632, ... [4530621225134718980, 4514858626438922245, 451... MULTIPOLYGON (((26.56505 17.54840, 28.42828 10...
1 53950935 Africa Tanzania TZA 150600.0 POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... [4365113938828853253, 4379750637617807365, 438... MULTIPOLYGON (((33.58542 -2.71514, 35.28235 -5...
58 7965055 Africa Togo TGO 11610.0 POLYGON ((0.89956 10.99734, 0.77234 10.47081, ... [4415779434636771335, 4415814619008860167, 441... MULTIPOLYGON (((0.41556 6.86747, 0.77508 6.243...
81 11403800 Africa Tunisia TUN 130800.0 POLYGON ((9.48214 30.30756, 9.05560 32.10269, ... [4592404982522707974, 4549198573597622279, 454... MULTIPOLYGON (((9.75682 31.32854, 11.26648 33....
168 39570125 Africa Uganda UGA 84930.0 POLYGON ((33.90371 -0.95000, 31.86617 -1.02736... [4361876976596680710, 4362017714085036038, 436... MULTIPOLYGON (((31.89581 -0.10339, 32.90162 0....
2 603253 Africa W. Sahara ESH 906.5 POLYGON ((-8.66559 27.65643, -8.66512 27.58948... [4230005950007738375, 4230041134379827207, 423... MULTIPOLYGON (((-10.96790 27.09447, -10.58624 ...
70 15972000 Africa Zambia ZMB 65170.0 POLYGON ((30.74001 -8.34001, 31.15775 -8.59458... [4332462841530417157, 4377498837804122117, 437... MULTIPOLYGON (((28.67505 -15.69986, 24.61735 -...
48 13805084 Africa Zimbabwe ZWE 28330.0 POLYGON ((31.19141 -22.25151, 30.65987 -22.151... [118782440171896837, 18295873486192646, 819092... MULTIPOLYGON (((31.25060 -21.50281, 31.18842 -...
73 1467152 Africa eSwatini SWZ 11060.0 POLYGON ((32.07167 -26.73382, 31.86806 -27.177... [120049077567094791, 120084261939183623, 12011... MULTIPOLYGON (((31.32931 -25.44421, 31.35276 -...
[18]:
fig, ax = plt.subplots(figsize=(5,5), dpi=100)
ax.grid(True)

africa.plot(ax=ax, trixels=True, boundary=True, column='name', linewidth=0.2)
[18]:
<AxesSubplot:>
../../_images/examples_notebooks_instantiation_30_1.png

Holes in RSA#

[19]:
rsa = africa[africa.name=='South Africa']
[20]:
fig, ax = plt.subplots()
ax.grid(True)

rsa.plot(ax=ax, trixels=True, boundary=True, color='y', lw=1, zorder=0)
rsa.plot(ax=ax, trixels=False, facecolor="none", edgecolor='blue', lw=1, zorder=1)
[20]:
<AxesSubplot:>
../../_images/examples_notebooks_instantiation_33_1.png
[21]:
p = rsa.iloc[0].geometry
interior = starepandas.sids_from_ring(p.interiors[0], level=12, force_ccw=False)

Puerto Rico (GPKG)#

[22]:
file = '../tests/data/caribbean.gpkg'
gdf = geopandas.read_file(file)

# Sanitation
gdf.rename(columns={'NAME_0': "name"}, inplace=True)
gdf.sort_values(by='name', inplace=True)
gdf.set_index('ISO', inplace=True)
gdf = gdf[['name', 'geometry']]

# Adding simple geom
gdf['geom_simple'] = gdf.simplify(0.001)
gdf.set_geometry('geom_simple', inplace=True)
[23]:
gdf = gdf[gdf['name']=='Puerto Rico']
sdf = starepandas.STAREDataFrame(gdf)
[24]:
sids = starepandas.sids_from_gdf(sdf, level=14, force_ccw=True)
sdf.set_sids(sids, inplace=True)
[25]:
trixels = sdf.make_trixels()
sdf.set_trixels(trixels, inplace=True)
[26]:
fig, ax = plt.subplots(figsize=(13,5), dpi=100)
ax.grid(True)

sdf.plot(ax=ax, trixels=True, color='y', lw=0.5, zorder=0)
sdf.plot(ax=ax, trixels=False, facecolor="none", edgecolor='blue', lw=0.5, zorder=1)
[26]:
<AxesSubplot:>
../../_images/examples_notebooks_instantiation_40_1.png
[ ]: