In [1]:
from iSDM.species import GBIFSpecies

In [2]:
my_species = GBIFSpecies(name_species="Etheostoma_blennioides")

In [3]:
my_data = my_species.load_data("Etheostoma_blennioides2382397.pkl") # this is a reduced dataset

In [4]:
%matplotlib inline

my_species.polygonize().plot() # crashes if NaN coordinates. osgeo's fault; fixed to ignore them


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a69142048>

In [5]:
my_species.data_full.geometry.plot()


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a69142160>

In [42]:
from shapely.geometry import Point, Polygon
# cut the outliers, i.e., overlay with a polygon
my_species.data_full = my_species.data_full[my_species.data_full.geometry.within(Polygon(((-100,30), (-100, 50), (-70, 50),(-70, 30))))]

In [43]:
my_species.polygonize().plot()


Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5c2cc8d0>

In [44]:
my_species.polygonize(buffer=0.5, simplify_tolerance=0.05).plot()


Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5c0ff550>

In [45]:
my_species.polygonize(buffer=0.3, simplify_tolerance=0.03).plot()  # etc


Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5c0b07f0>

In [41]:
filtered_data = my_species.data_full.geometry[my_species.data_full.geometry.within(Polygon(((-100,30), (-100, 50), (-70, 50),(-70, 30))))]
filtered_data.plot()


Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5ece2ac8>

In [11]:
my_species.polygonize(buffer=0.3, simplify_tolerance=0.03, with_envelope=True).plot() # with_envelope means pixelized


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5f130400>

In [63]:
type(filtered_data)


Out[63]:
geopandas.geoseries.GeoSeries

In [66]:
my_species.data_full.shape


Out[66]:
(5226, 138)

In [67]:
filtered_data.shape


Out[67]:
(3333,)

In [56]:
# cut even more
my_species.polygonize(buffer=0.3, simplify_tolerance=0.03, with_envelope=True).geometry.intersection(Polygon(((-90,40), (-90, 50), (-70, 50),(-70, 40)))).plot()


Out[56]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a5be535f8>

In [48]:
Polygon(((-100,30), (-100, 50), (-70, 50),(-70, 30)))


Out[48]:

In [57]:
my_species.polygonize(buffer=0.3, simplify_tolerance=0.03, with_envelope=True).geometry.intersection(Polygon(((-90,40), (-90, 50), (-70, 50),(-70, 40))))


Out[57]:
0                                                   ()
1                                                   ()
2                                                   ()
3                                                   ()
4    POLYGON ((-74.10083 42.39859000000001, -74.100...
5    POLYGON ((-80.2 42.51667, -80.8 42.51667, -80....
6    POLYGON ((-78.12052 42.97298, -78.12052 43.572...
7    POLYGON ((-89.12281999999999 40, -89.122819999...
dtype: object

In [108]:
from geopandas import GeoDataFrame

In [109]:
from shapely.geometry import Point

In [121]:
crs = None # converting GBIF pointrecords to geopandas; maybe a good idea to have it unified
geometry = [Point(xy) for xy in zip(my_data['decimallongitude'], my_data['decimallatitude'])]

In [122]:
geo_df = GeoDataFrame(my_data, crs=crs, geometry=geometry)

In [112]:
%matplotlib inline
geo_df.plot(figsize=(20, 25))


Out[112]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3fefe48>

In [113]:
geo_df.centroid.plot(figsize=(20,25)) # nope, still points


Out[113]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3b41d68>

In [123]:
my_data


Out[123]:
decimallatitude decimallongitude rightsholder datasetname geometry
1 35.53250 -87.29580 Yale Peabody Museum of Natural History NaN POINT (-87.2958 35.5325)
2 41.79664 -80.97289 Robert L Curtis iNaturalist research-grade observations POINT (-80.97289000000001 41.79664)
3 37.97240 -83.56716 Brian Wulker iNaturalist research-grade observations POINT (-83.56716 37.9724)
4 36.77431 -84.88030 Yale Peabody Museum of Natural History NaN POINT (-84.88030000000001 36.77431)
5 36.63478 -85.14578 Yale Peabody Museum of Natural History NaN POINT (-85.14578 36.63478)
6 36.73364 -85.22733 Yale Peabody Museum of Natural History NaN POINT (-85.22732999999999 36.73364)
7 36.63478 -85.14578 Yale Peabody Museum of Natural History NaN POINT (-85.14578 36.63478)
8 34.58414 -87.70333 Yale Peabody Museum of Natural History NaN POINT (-87.70332999999999 34.58414)
9 35.39114 -82.56875 Yale Peabody Museum of Natural History NaN POINT (-82.56874999999999 35.39114)
10 35.53244 -87.29580 Yale Peabody Museum of Natural History NaN POINT (-87.2958 35.53244)
11 35.17770 -83.88780 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.8878 35.17769999911117)
12 40.30320 -82.78440 NaN NaN POINT (-82.78440000000001 40.3032)
13 39.32950 -82.12280 NaN NaN POINT (-82.1228 39.3295)
14 35.07780 -83.97430 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.9743 35.07779999911228)
15 36.97886 -82.98860 Yale Peabody Museum of Natural History NaN POINT (-82.98860000000001 36.97886)
16 39.11120 -81.73990 NaN NaN POINT (-81.73990000000001 39.1112)
17 36.72267 -83.55836 Yale Peabody Museum of Natural History NaN POINT (-83.55835999999999 36.72267)
18 35.16030 -83.92020 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.92019999999999 35.1603)
19 36.64956 -83.65600 Yale Peabody Museum of Natural History NaN POINT (-83.65600000000001 36.64956)
20 37.03580 -84.10619 Yale Peabody Museum of Natural History NaN POINT (-84.10619 37.0358)
21 42.22403 -76.33849 NaN NaN POINT (-76.33848999999999 42.22403)
22 39.28230 -82.75070 NaN NaN POINT (-82.75069999999999 39.2823)
23 39.47140 -81.11140 NaN NaN POINT (-81.1114 39.4714)
24 39.26990 -81.59540 NaN NaN POINT (-81.5954 39.2699)
25 41.72430 -81.17680 NaN NaN POINT (-81.1768 41.7243)
26 35.46300 -87.53600 NaN NaN POINT (-87.536 35.463)
27 35.94170 -86.37790 Sam Noble Oklahoma Museum of Natural History NaN POINT (-86.3779 35.9417)
28 36.05950 -86.94460 Sam Noble Oklahoma Museum of Natural History NaN POINT (-86.94459999999999 36.0595)
29 36.10150 -87.10640 Sam Noble Oklahoma Museum of Natural History NaN POINT (-87.10639999999999 36.1015)
30 36.41300 -81.40710 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-81.4071 36.41299999909837)
... ... ... ... ... ...
91 40.84240 -83.30440 NaN NaN POINT (-83.3044 40.8424)
92 37.30560 -80.62120 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-80.6212 37.3056)
93 42.00977 -85.79363 NaN Auburn University Museum Fish Collection POINT (-85.79362999999999 42.00977)
94 37.12913 -94.62878 NaN Fort Hays Sternberg Museum Ichthyology Collection POINT (-94.62878000000001 37.12913)
95 40.53344 -76.39342 NaN NaN POINT (-76.39342000000001 40.53344)
96 37.12537 -94.61963 NaN Fort Hays Sternberg Museum Ichthyology Collection POINT (-94.61963 37.12537)
97 40.62222 -77.36111 NaN NaN POINT (-77.36111 40.62222)
98 41.69290 -84.68780 NaN NaN POINT (-84.6878 41.6929)
99 40.41920 -77.15200 NaN NaN POINT (-77.152 40.4192)
100 40.42556 -77.13139 NaN NaN POINT (-77.13139 40.42556)
101 34.98710 -85.95412 NaN Auburn University Museum Fish Collection POINT (-85.95412 34.9871)
102 41.56111 -75.87028 NaN NaN POINT (-75.87027999999999 41.56111)
103 39.94720 -83.84540 NaN NaN POINT (-83.8454 39.9472)
104 39.81610 -86.75330 NaN NaN POINT (-86.7533 39.8161)
105 40.09660 -76.63642 NaN NaN POINT (-76.63642 40.0966)
106 39.02200 -82.87870 NaN NaN POINT (-82.87869999999999 39.022)
107 40.42944 -77.09500 NaN NaN POINT (-77.095 40.42944)
108 35.15883 -84.46855 NaN Auburn University Museum Fish Collection POINT (-84.46854999999999 35.15883)
109 38.72640 -83.15420 NaN NaN POINT (-83.1542 38.7264)
110 39.55470 -83.00080 NaN NaN POINT (-83.0008 39.5547)
111 40.61480 -80.64380 NaN NaN POINT (-80.6438 40.6148)
112 34.77522 -86.25060 NaN Auburn University Museum Fish Collection POINT (-86.25060000000001 34.77522)
113 34.91407 -86.42754 NaN Auburn University Museum Fish Collection POINT (-86.42753999999999 34.91407)
114 36.07914 -83.89869 NaN Auburn University Museum Fish Collection POINT (-83.89869 36.07914)
115 39.72600 -80.85900 NaN NaN POINT (-80.85899999999999 39.726)
116 40.42830 -80.61430 NaN NaN POINT (-80.6143 40.4283)
117 40.32810 -81.96460 NaN NaN POINT (-81.9646 40.3281)
118 34.58210 -86.30605 NaN Auburn University Museum Fish Collection POINT (-86.30605 34.5821)
119 37.03930 -82.84120 NaN NaN POINT (-82.8412 37.0393)
120 35.16055 -84.46810 NaN Auburn University Museum Fish Collection POINT (-84.46810000000001 35.16055)

119 rows × 5 columns


In [126]:
geo_df


Out[126]:
decimallatitude decimallongitude rightsholder datasetname geometry
1 35.53250 -87.29580 Yale Peabody Museum of Natural History NaN POINT (-87.2958 35.5325)
2 41.79664 -80.97289 Robert L Curtis iNaturalist research-grade observations POINT (-80.97289000000001 41.79664)
3 37.97240 -83.56716 Brian Wulker iNaturalist research-grade observations POINT (-83.56716 37.9724)
4 36.77431 -84.88030 Yale Peabody Museum of Natural History NaN POINT (-84.88030000000001 36.77431)
5 36.63478 -85.14578 Yale Peabody Museum of Natural History NaN POINT (-85.14578 36.63478)
6 36.73364 -85.22733 Yale Peabody Museum of Natural History NaN POINT (-85.22732999999999 36.73364)
7 36.63478 -85.14578 Yale Peabody Museum of Natural History NaN POINT (-85.14578 36.63478)
8 34.58414 -87.70333 Yale Peabody Museum of Natural History NaN POINT (-87.70332999999999 34.58414)
9 35.39114 -82.56875 Yale Peabody Museum of Natural History NaN POINT (-82.56874999999999 35.39114)
10 35.53244 -87.29580 Yale Peabody Museum of Natural History NaN POINT (-87.2958 35.53244)
11 35.17770 -83.88780 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.8878 35.17769999911117)
12 40.30320 -82.78440 NaN NaN POINT (-82.78440000000001 40.3032)
13 39.32950 -82.12280 NaN NaN POINT (-82.1228 39.3295)
14 35.07780 -83.97430 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.9743 35.07779999911228)
15 36.97886 -82.98860 Yale Peabody Museum of Natural History NaN POINT (-82.98860000000001 36.97886)
16 39.11120 -81.73990 NaN NaN POINT (-81.73990000000001 39.1112)
17 36.72267 -83.55836 Yale Peabody Museum of Natural History NaN POINT (-83.55835999999999 36.72267)
18 35.16030 -83.92020 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-83.92019999999999 35.1603)
19 36.64956 -83.65600 Yale Peabody Museum of Natural History NaN POINT (-83.65600000000001 36.64956)
20 37.03580 -84.10619 Yale Peabody Museum of Natural History NaN POINT (-84.10619 37.0358)
21 42.22403 -76.33849 NaN NaN POINT (-76.33848999999999 42.22403)
22 39.28230 -82.75070 NaN NaN POINT (-82.75069999999999 39.2823)
23 39.47140 -81.11140 NaN NaN POINT (-81.1114 39.4714)
24 39.26990 -81.59540 NaN NaN POINT (-81.5954 39.2699)
25 41.72430 -81.17680 NaN NaN POINT (-81.1768 41.7243)
26 35.46300 -87.53600 NaN NaN POINT (-87.536 35.463)
27 35.94170 -86.37790 Sam Noble Oklahoma Museum of Natural History NaN POINT (-86.3779 35.9417)
28 36.05950 -86.94460 Sam Noble Oklahoma Museum of Natural History NaN POINT (-86.94459999999999 36.0595)
29 36.10150 -87.10640 Sam Noble Oklahoma Museum of Natural History NaN POINT (-87.10639999999999 36.1015)
30 36.41300 -81.40710 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-81.4071 36.41299999909837)
... ... ... ... ... ...
91 40.84240 -83.30440 NaN NaN POINT (-83.3044 40.8424)
92 37.30560 -80.62120 North Carolina Museum of Natural Sciences NCSM Fishes Collection POINT (-80.6212 37.3056)
93 42.00977 -85.79363 NaN Auburn University Museum Fish Collection POINT (-85.79362999999999 42.00977)
94 37.12913 -94.62878 NaN Fort Hays Sternberg Museum Ichthyology Collection POINT (-94.62878000000001 37.12913)
95 40.53344 -76.39342 NaN NaN POINT (-76.39342000000001 40.53344)
96 37.12537 -94.61963 NaN Fort Hays Sternberg Museum Ichthyology Collection POINT (-94.61963 37.12537)
97 40.62222 -77.36111 NaN NaN POINT (-77.36111 40.62222)
98 41.69290 -84.68780 NaN NaN POINT (-84.6878 41.6929)
99 40.41920 -77.15200 NaN NaN POINT (-77.152 40.4192)
100 40.42556 -77.13139 NaN NaN POINT (-77.13139 40.42556)
101 34.98710 -85.95412 NaN Auburn University Museum Fish Collection POINT (-85.95412 34.9871)
102 41.56111 -75.87028 NaN NaN POINT (-75.87027999999999 41.56111)
103 39.94720 -83.84540 NaN NaN POINT (-83.8454 39.9472)
104 39.81610 -86.75330 NaN NaN POINT (-86.7533 39.8161)
105 40.09660 -76.63642 NaN NaN POINT (-76.63642 40.0966)
106 39.02200 -82.87870 NaN NaN POINT (-82.87869999999999 39.022)
107 40.42944 -77.09500 NaN NaN POINT (-77.095 40.42944)
108 35.15883 -84.46855 NaN Auburn University Museum Fish Collection POINT (-84.46854999999999 35.15883)
109 38.72640 -83.15420 NaN NaN POINT (-83.1542 38.7264)
110 39.55470 -83.00080 NaN NaN POINT (-83.0008 39.5547)
111 40.61480 -80.64380 NaN NaN POINT (-80.6438 40.6148)
112 34.77522 -86.25060 NaN Auburn University Museum Fish Collection POINT (-86.25060000000001 34.77522)
113 34.91407 -86.42754 NaN Auburn University Museum Fish Collection POINT (-86.42753999999999 34.91407)
114 36.07914 -83.89869 NaN Auburn University Museum Fish Collection POINT (-83.89869 36.07914)
115 39.72600 -80.85900 NaN NaN POINT (-80.85899999999999 39.726)
116 40.42830 -80.61430 NaN NaN POINT (-80.6143 40.4283)
117 40.32810 -81.96460 NaN NaN POINT (-81.9646 40.3281)
118 34.58210 -86.30605 NaN Auburn University Museum Fish Collection POINT (-86.30605 34.5821)
119 37.03930 -82.84120 NaN NaN POINT (-82.8412 37.0393)
120 35.16055 -84.46810 NaN Auburn University Museum Fish Collection POINT (-84.46810000000001 35.16055)

119 rows × 5 columns


In [127]:
type(my_data)


Out[127]:
pandas.core.frame.DataFrame

In [128]:
my_data['decimallatitude'].head()


Out[128]:
1    35.53250
2    41.79664
3    37.97240
4    36.77431
5    36.63478
Name: decimallatitude, dtype: float64

In [129]:
my_data.head() # notice now the geometry column is there, automatically appended


Out[129]:
decimallatitude decimallongitude rightsholder datasetname geometry
1 35.53250 -87.29580 Yale Peabody Museum of Natural History NaN POINT (-87.2958 35.5325)
2 41.79664 -80.97289 Robert L Curtis iNaturalist research-grade observations POINT (-80.97289000000001 41.79664)
3 37.97240 -83.56716 Brian Wulker iNaturalist research-grade observations POINT (-83.56716 37.9724)
4 36.77431 -84.88030 Yale Peabody Museum of Natural History NaN POINT (-84.88030000000001 36.77431)
5 36.63478 -85.14578 Yale Peabody Museum of Natural History NaN POINT (-85.14578 36.63478)

In [79]:
type(my_data)


Out[79]:
pandas.core.frame.DataFrame

In [130]:
geo_df.shape


Out[130]:
(119, 5)

In [131]:
my_data.shape


Out[131]:
(119, 5)

In [132]:
my_data.shape == geo_df.shape # hmmm


Out[132]:
True

In [133]:
geo_df.geometry # geometry in GBIF data is POINT of course, geometry column in IUCN is polygon.
# but can't we make a polygon out of points? Like the outer boundary or something?


Out[133]:
1                 POINT (-87.2958 35.5325)
2      POINT (-80.97289000000001 41.79664)
3                POINT (-83.56716 37.9724)
4      POINT (-84.88030000000001 36.77431)
5               POINT (-85.14578 36.63478)
6      POINT (-85.22732999999999 36.73364)
7               POINT (-85.14578 36.63478)
8      POINT (-87.70332999999999 34.58414)
9      POINT (-82.56874999999999 35.39114)
10               POINT (-87.2958 35.53244)
11      POINT (-83.8878 35.17769999911117)
12      POINT (-82.78440000000001 40.3032)
13                POINT (-82.1228 39.3295)
14      POINT (-83.9743 35.07779999911228)
15     POINT (-82.98860000000001 36.97886)
16      POINT (-81.73990000000001 39.1112)
17     POINT (-83.55835999999999 36.72267)
18      POINT (-83.92019999999999 35.1603)
19     POINT (-83.65600000000001 36.64956)
20               POINT (-84.10619 37.0358)
21     POINT (-76.33848999999999 42.22403)
22      POINT (-82.75069999999999 39.2823)
23                POINT (-81.1114 39.4714)
24                POINT (-81.5954 39.2699)
25                POINT (-81.1768 41.7243)
26                  POINT (-87.536 35.463)
27                POINT (-86.3779 35.9417)
28      POINT (-86.94459999999999 36.0595)
29      POINT (-87.10639999999999 36.1015)
30      POINT (-81.4071 36.41299999909837)
                      ...                 
91                POINT (-83.3044 40.8424)
92                POINT (-80.6212 37.3056)
93     POINT (-85.79362999999999 42.00977)
94     POINT (-94.62878000000001 37.12913)
95     POINT (-76.39342000000001 40.53344)
96              POINT (-94.61963 37.12537)
97              POINT (-77.36111 40.62222)
98                POINT (-84.6878 41.6929)
99                 POINT (-77.152 40.4192)
100             POINT (-77.13139 40.42556)
101              POINT (-85.95412 34.9871)
102    POINT (-75.87027999999999 41.56111)
103               POINT (-83.8454 39.9472)
104               POINT (-86.7533 39.8161)
105              POINT (-76.63642 40.0966)
106      POINT (-82.87869999999999 39.022)
107               POINT (-77.095 40.42944)
108    POINT (-84.46854999999999 35.15883)
109               POINT (-83.1542 38.7264)
110               POINT (-83.0008 39.5547)
111               POINT (-80.6438 40.6148)
112    POINT (-86.25060000000001 34.77522)
113    POINT (-86.42753999999999 34.91407)
114             POINT (-83.89869 36.07914)
115      POINT (-80.85899999999999 39.726)
116               POINT (-80.6143 40.4283)
117               POINT (-81.9646 40.3281)
118              POINT (-86.30605 34.5821)
119               POINT (-82.8412 37.0393)
120    POINT (-84.46810000000001 35.16055)
Name: geometry, dtype: object

In [134]:
geo_df.geometry.area # right, this is why it won't work, its a series of points, not polygons.


Out[134]:
1      0.0
2      0.0
3      0.0
4      0.0
5      0.0
6      0.0
7      0.0
8      0.0
9      0.0
10     0.0
11     0.0
12     0.0
13     0.0
14     0.0
15     0.0
16     0.0
17     0.0
18     0.0
19     0.0
20     0.0
21     0.0
22     0.0
23     0.0
24     0.0
25     0.0
26     0.0
27     0.0
28     0.0
29     0.0
30     0.0
      ... 
91     0.0
92     0.0
93     0.0
94     0.0
95     0.0
96     0.0
97     0.0
98     0.0
99     0.0
100    0.0
101    0.0
102    0.0
103    0.0
104    0.0
105    0.0
106    0.0
107    0.0
108    0.0
109    0.0
110    0.0
111    0.0
112    0.0
113    0.0
114    0.0
115    0.0
116    0.0
117    0.0
118    0.0
119    0.0
120    0.0
dtype: float64

In [135]:
geo_df.geometry[1]


Out[135]:

In [136]:
my_data.geometry[1].buffer(2) #yay


Out[136]:

In [137]:
geo_df.geometry.buffer(2).plot()


Out[137]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee39be6a0>

In [138]:
geo_df.buffer(0.5).plot(figsize=(20,25)) # a buffer around each point record; we could draw pseudo-absences from there


Out[138]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3640fd0>

In [139]:
type(geo_df.geometry[1])


Out[139]:
shapely.geometry.point.Point

In [140]:
from iSDM.species import IUCNSpecies
turtles = IUCNSpecies(name_species="Acanthochelys_pallidipectoris")

In [141]:
turtles_df = turtles.load_data("/home/daniela/git/iSDM/notebooks/Acanthochelys pallidipectoris75.pkl")

In [142]:
turtles_df


Out[142]:
binomial category citation class_name compiler dist_comm family_nam genus_name geometry id_no ... presence seasonal shape_Area shape_Leng source species_na subpop subspecies tax_comm year
138 Acanthochelys pallidipectoris VU CRF REPTILIA Rhodin None CHELIDAE Acanthochelys POLYGON ((-59.20001220703125 -28.0374755859375... 75.0 ... 1.0 1.0 36.632836 54.770042 CBFTT pallidipectoris None None None 2013.0

1 rows × 26 columns


In [143]:
turtles_df.geometry


Out[143]:
138    POLYGON ((-59.20001220703125 -28.0374755859375...
Name: geometry, dtype: object

In [144]:
type(turtles_df.geometry)


Out[144]:
geopandas.geoseries.GeoSeries

In [145]:
type(geo_df.geometry)


Out[145]:
geopandas.geoseries.GeoSeries

In [146]:
turtles_df.geometry.bounds


Out[146]:
minx miny maxx maxy
138 -65.141846 -30.608276 -57.536926 -21.49585

In [147]:
turtles_df.geometry.length


Out[147]:
138    54.772857
dtype: float64

In [148]:
geo_df.geometry.bounds.head() # bounds == points, nothing changes. but at least useful to get individual x y coords


Out[148]:
minx miny maxx maxy
1 -87.29580 35.53250 -87.29580 35.53250
2 -80.97289 41.79664 -80.97289 41.79664
3 -83.56716 37.97240 -83.56716 37.97240
4 -84.88030 36.77431 -84.88030 36.77431
5 -85.14578 36.63478 -85.14578 36.63478

In [150]:
turtles_df.representative_point() # could be useful for sampling?


Out[150]:
138    POINT (-61.16457330175193 -26.05206298828125)
dtype: object

In [151]:
turtles_df.geometry.plot()


Out[151]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee33b76a0>

In [152]:
geo_df.geometry.representative_point().head()


Out[152]:
1               POINT (-87.2958 35.5325)
2    POINT (-80.97289000000001 41.79664)
3              POINT (-83.56716 37.9724)
4    POINT (-84.88030000000001 36.77431)
5             POINT (-85.14578 36.63478)
dtype: object

In [153]:
geo_df.geometry.head() # representative point of a point, well, still.


Out[153]:
1               POINT (-87.2958 35.5325)
2    POINT (-80.97289000000001 41.79664)
3              POINT (-83.56716 37.9724)
4    POINT (-84.88030000000001 36.77431)
5             POINT (-85.14578 36.63478)
Name: geometry, dtype: object

In [154]:
turtles_df.geometry.is_valid


Ring Self-intersection at or near point -59.20001220703125 -28.0374755859375
Out[154]:
138    False
dtype: bool

In [155]:
turtles_df.contains(Point(-61,-26))


Out[155]:
138    True
dtype: bool

In [156]:
turtles_df.geometry.boundary.plot()


Out[156]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee33b3278>

In [157]:
turtles_df.difference(Point(-61, -26)).plot(figsize=(24,26))


Out[157]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee32da198>

In [158]:
turtles_df.iat[0,8]


Ring Self-intersection at or near point -59.20001220703125 -28.0374755859375
Out[158]:

In [159]:
turtles_df.geometry.convex_hull.plot()


Out[159]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3252668>

In [160]:
turtles_df.geometry.envelope.plot()


Out[160]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee31c0a58>

In [161]:
turtles_df.simplify(tolerance=0.3).plot() # nice!


Out[161]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3161550>

In [162]:
turtles_df.crs # !!!! <-- get the coordinate reference system.


Out[162]:
{'init': 'epsg:4326'}

In [164]:
shapely_point = geo_df.geometry[1]
shapely_point.xy


Out[164]:
(array('d', [-87.2958]), array('d', [35.5325]))

In [165]:
shapely_point.buffer(2).area


Out[165]:
12.546193962183764

In [166]:
# ONE WAY OF CREATING A POLYGON
shapely_buffer = shapely_point.buffer(2.0)
shapely_buffer.simplify(0.2, preserve_topology=False)


Out[166]:

In [167]:
type(shapely_buffer) # already a polygon


Out[167]:
shapely.geometry.polygon.Polygon

In [170]:
# merge with another point
shapely_buffer.simplify(0.2, preserve_topology=True).union( geo_df.geometry[5].buffer(2).simplify(0.2, preserve_topology=False))


Out[170]:

In [171]:
# merge with another point
another_polygon = shapely_buffer.simplify(0.2, preserve_topology=False).union( geo_df.geometry[10].buffer(2).simplify(0.2, preserve_topology=False))
another_polygon.union( geo_df.geometry[100].buffer(2).simplify(0.2, preserve_topology=True))


Out[171]:

In [172]:
shapely_buffer.exterior.coords.xy


Out[172]:
(array('d', [-85.2958, -85.3054305466556, -85.33422943919354, -85.38191932853559, -85.44804093497743, -85.53195747130329, -85.6328607753949, -85.74977909327453, -85.88158643762691, -86.02701343167271, -86.1846595339608, -86.353006526348, -86.53043313526982, -86.71523064549108, -86.90561935596774, -87.09976571934088, -87.2958, -87.49183428065912, -87.68598064403226, -87.87636935450892, -88.06116686473018, -88.23859347365199, -88.4069404660392, -88.56458656832729, -88.71001356237309, -88.84182090672547, -88.95873922460508, -89.05964252869671, -89.14355906502257, -89.20968067146441, -89.25737056080646, -89.2861694533444, -89.2958, -89.2861694533444, -89.25737056080646, -89.20968067146441, -89.14355906502257, -89.05964252869671, -88.9587392246051, -88.84182090672547, -88.71001356237309, -88.56458656832729, -88.4069404660392, -88.238593473652, -88.06116686473018, -87.87636935450892, -87.68598064403226, -87.49183428065912, -87.2958, -87.09976571934088, -86.90561935596774, -86.71523064549108, -86.53043313526983, -86.35300652634801, -86.1846595339608, -86.02701343167271, -85.88158643762691, -85.74977909327454, -85.63286077539492, -85.5319574713033, -85.44804093497743, -85.38191932853559, -85.33422943919354, -85.30543054665561, -85.2958, -85.2958]),
 array('d', [35.5325, 35.33646571934088, 35.14231935596774, 34.951930645491075, 34.76713313526982, 34.589706526348, 34.421359533960796, 34.263713431672706, 34.11828643762691, 33.986479093274525, 33.86956077539491, 33.76865747130329, 33.68474093497743, 33.61861932853558, 33.57092943919354, 33.542130546655606, 33.5325, 33.542130546655606, 33.570929439193534, 33.61861932853558, 33.68474093497743, 33.768657471303285, 33.86956077539491, 33.986479093274525, 34.1182864376269, 34.263713431672706, 34.42135953396079, 34.589706526348, 34.767133135269816, 34.951930645491075, 35.14231935596774, 35.33646571934088, 35.5325, 35.72853428065912, 35.922680644032255, 36.11306935450892, 36.297866864730175, 36.475293473651995, 36.6436404660392, 36.80128656832729, 36.94671356237309, 37.07852090672547, 37.19543922460509, 37.296342528696705, 37.38025906502257, 37.44638067146442, 37.494070560806456, 37.52286945334439, 37.5325, 37.52286945334439, 37.49407056080646, 37.44638067146442, 37.38025906502258, 37.29634252869671, 37.195439224605096, 37.07852090672548, 36.946713562373105, 36.8012865683273, 36.643640466039216, 36.47529347365201, 36.29786686473019, 36.11306935450894, 35.92268064403227, 35.72853428065913, 35.53250000000001, 35.5325]))

In [173]:
import shapely.ops

In [229]:
third_polygon = geo_df.geometry[30].buffer(1, resolution=160).simplify(0.1, preserve_topology=False)

In [230]:
third_polygon


Out[230]:

In [191]:
fourth_polygon = geo_df.geometry[50].buffer(2).simplify(0.2, preserve_topology=False)

In [192]:
fourth_polygon


Out[192]:

In [193]:
shapely.ops.cascaded_union([another_polygon, third_polygon, fourth_polygon])


Out[193]:

In [194]:
type(shapely.ops.cascaded_union([another_polygon, third_polygon]))


Out[194]:
shapely.geometry.multipolygon.MultiPolygon

In [195]:
multipolyon = shapely.ops.cascaded_union([another_polygon, third_polygon, fourth_polygon])
multipolyon.convex_hull


Out[195]:

In [196]:
third_polygon.envelope # or we can "pixelize" every point (with a buffer around) like this, and then combine the envelopes


Out[196]:

In [197]:
multipolyon_envelope = shapely.ops.cascaded_union([another_polygon.envelope, third_polygon.envelope, fourth_polygon.envelope])

In [200]:
multipolyon_envelope


Out[200]:

In [201]:
multipolyon_envelope.convex_hull


Out[201]:

In [233]:
geo_df.geometry.buffer(1, resolution=160).simplify(0.1, preserve_topology=False)


Out[233]:
1      POLYGON ((-86.2958 35.5325, -86.58869321881345...
2      POLYGON ((-79.97289000000001 41.79664, -80.265...
3      POLYGON ((-82.56716 37.9724, -82.8600532188134...
4      POLYGON ((-83.88030000000001 36.77431, -84.173...
5      POLYGON ((-84.14578 36.63478, -84.438673218813...
6      POLYGON ((-84.22732999999999 36.73364, -84.520...
7      POLYGON ((-84.14578 36.63478, -84.438673218813...
8      POLYGON ((-86.70332999999999 34.58414, -86.996...
9      POLYGON ((-81.56874999999999 35.39114, -81.861...
10     POLYGON ((-86.2958 35.53244, -86.5886932188134...
11     POLYGON ((-82.8878 35.17769999911117, -83.1806...
12     POLYGON ((-81.78440000000001 40.3032, -82.0772...
13     POLYGON ((-81.1228 39.3295, -81.41569321881344...
14     POLYGON ((-82.9743 35.07779999911228, -83.2671...
15     POLYGON ((-81.98860000000001 36.97886, -82.281...
16     POLYGON ((-80.73990000000001 39.1112, -81.0327...
17     POLYGON ((-82.55835999999999 36.72267, -82.851...
18     POLYGON ((-82.92019999999999 35.1603, -83.2130...
19     POLYGON ((-82.65600000000001 36.64956, -82.948...
20     POLYGON ((-83.10619 37.0358, -83.3990832188134...
21     POLYGON ((-75.33848999999999 42.22403, -75.631...
22     POLYGON ((-81.75069999999999 39.2823, -82.0435...
23     POLYGON ((-80.1114 39.4714, -80.40429321881345...
24     POLYGON ((-80.5954 39.2699, -80.88829321881344...
25     POLYGON ((-80.1768 41.7243, -80.46969321881345...
26     POLYGON ((-86.536 35.463, -86.82889321881345 3...
27     POLYGON ((-85.3779 35.9417, -85.67079321881344...
28     POLYGON ((-85.94459999999999 36.0595, -86.2374...
29     POLYGON ((-86.10639999999999 36.1015, -86.3992...
30     POLYGON ((-80.4071 36.41299999909837, -80.6999...
                             ...                        
91     POLYGON ((-82.3044 40.8424, -82.59729321881345...
92     POLYGON ((-79.6212 37.3056, -79.91409321881345...
93     POLYGON ((-84.79362999999999 42.00977, -85.086...
94     POLYGON ((-93.62878000000001 37.12913, -93.921...
95     POLYGON ((-75.39342000000001 40.53344, -75.686...
96     POLYGON ((-93.61963 37.12537, -93.912523218813...
97     POLYGON ((-76.36111 40.62222, -76.654003218813...
98     POLYGON ((-83.6878 41.6929, -83.98069321881344...
99     POLYGON ((-76.152 40.4192, -76.44489321881345 ...
100    POLYGON ((-76.13139 40.42556, -76.424283218813...
101    POLYGON ((-84.95412 34.9871, -85.2470132188134...
102    POLYGON ((-74.87027999999999 41.56111, -75.163...
103    POLYGON ((-82.8454 39.9472, -83.13829321881344...
104    POLYGON ((-85.7533 39.8161, -86.04619321881344...
105    POLYGON ((-75.63642 40.0966, -75.9293132188134...
106    POLYGON ((-81.87869999999999 39.022, -82.17159...
107    POLYGON ((-76.095 40.42944, -76.38789321881345...
108    POLYGON ((-83.46854999999999 35.15883, -83.761...
109    POLYGON ((-82.1542 38.7264, -82.44709321881345...
110    POLYGON ((-82.0008 39.5547, -82.29369321881344...
111    POLYGON ((-79.6438 40.6148, -79.93669321881345...
112    POLYGON ((-85.25060000000001 34.77522, -85.543...
113    POLYGON ((-85.42753999999999 34.91407, -85.720...
114    POLYGON ((-82.89869 36.07914, -83.191583218813...
115    POLYGON ((-79.85899999999999 39.726, -80.15189...
116    POLYGON ((-79.6143 40.4283, -79.90719321881345...
117    POLYGON ((-80.9646 40.3281, -81.25749321881345...
118    POLYGON ((-85.30605 34.5821, -85.5989432188134...
119    POLYGON ((-81.8412 37.0393, -82.13409321881345...
120    POLYGON ((-83.46810000000001 35.16055, -83.760...
dtype: object

In [262]:
# lets do it for all
multi_polygon = shapely.ops.cascaded_union(geo_df.geometry.buffer(1).simplify(0.1, preserve_topology=False).tolist())

In [263]:
multi_polygon


Out[263]:

In [264]:
multi_polygon.convex_hull


Out[264]:

In [265]:
multi_polygon_with_envelope = shapely.ops.cascaded_union(geo_df.geometry.buffer(1).simplify(0.1, preserve_topology=False).envelope.tolist())

In [266]:
multi_polygon_with_envelope


Out[266]:

In [267]:
multi_polygon_with_envelope.convex_hull


Out[267]:

In [268]:
shapely.ops.cascaded_union(geo_df.geometry.buffer(1).envelope.tolist()) # without simplify


Out[268]:

In [271]:
no_simplify = shapely.ops.cascaded_union(geo_df.geometry.buffer(1).tolist()) # without simplify

In [272]:
type(no_simplify)


Out[272]:
shapely.geometry.multipolygon.MultiPolygon

In [281]:
no_simplify.convex_hull.boundary


Out[281]:

In [283]:
turtles_df.geometry.plot(figsize=(20,20))


Out[283]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee3031da0>

In [284]:
type(turtles_df.geometry)


Out[284]:
geopandas.geoseries.GeoSeries

In [285]:
type(no_simplify.convex_hull)


Out[285]:
shapely.geometry.polygon.Polygon

In [307]:
geo_df_geometry = geo_df.copy(deep=True)

In [440]:
geo_df_geometry = geo_df_geometry.buffer(1).simplify(0.1, preserve_topology=False)

In [308]:
geo_df_geometry['geometry'] = geo_df_geometry['geometry'].apply(lambda x: x.buffer(1).simplify(0.1, preserve_topology=False))

In [309]:
geo_df_geometry.plot()


Out[309]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faee267d748>

In [85]:
# expanding each sample point into its polygon of influence (buffer)
geo_df.buffer(1).simplify(0.1, preserve_topology=False).plot() # no need for lambda like above


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-85-0ca63dc0dc71> in <module>()
      1 # expanding each sample point into its polygon of influence (buffer)
----> 2 geo_df.buffer(1).simplify(0.1, preserve_topology=False).plot() # no need for lambda like above

NameError: name 'geo_df' is not defined

In [325]:
shapely.ops.cascaded_union(geo_df_geometry.geometry.tolist()) # this is a multipolygon


Out[325]:

In [326]:
from geopandas import GeoDataFrame, GeoSeries

In [330]:
new_series = GeoSeries(shapely.ops.cascaded_union(geo_df_geometry.geometry.tolist()))

In [331]:
new_series.plot()


Out[331]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faed8067860>

In [333]:
new_series # not a multipolygon?


Out[333]:
0    (POLYGON ((-93.61963 37.12537, -93.91252321881...
dtype: object

In [337]:
type(shapely.ops.cascaded_union(geo_df_geometry.geometry))


Out[337]:
shapely.geometry.multipolygon.MultiPolygon

In [338]:
import pandas as pd

In [342]:
len(shapely.ops.cascaded_union(geo_df_geometry.geometry)) # HMMM, each island is a separate polygon


Out[342]:
5

In [347]:
shapely.ops.cascaded_union(geo_df_geometry.geometry)[4]


Out[347]:

In [348]:
shapely.ops.cascaded_union(geo_df_geometry.geometry)[2]


Out[348]:

In [380]:
my_multipolygon = shapely.ops.cascaded_union(geo_df_geometry.geometry)

In [381]:
type(my_multipolygon)


Out[381]:
shapely.geometry.multipolygon.MultiPolygon

In [382]:
len(my_multipolygon)


Out[382]:
5

In [392]:
again_series = GeoSeries(my_multipolygon)

In [393]:
again_series.shape # all in one polygon??


Out[393]:
(1,)

In [409]:
again_df = GeoDataFrame( geometry=[pol for pol in my_multipolygon]) # no .tolist for MultiPolygon unfortunatelly

In [410]:
again_df.plot() # YESS THANK YOU. Different islands different polygons


Out[410]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faed7b85ac8>

In [434]:
again_df.geometry.representative_point().plot()


Out[434]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faed6a89a20>

In [435]:
again_df.geometry.convex_hull.plot()


Out[435]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faed6a54438>

In [437]:
again_series.convex_hull.plot()


Out[437]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faed6a3ce48>

In [425]:
geo_df_geometry.geometry


Out[425]:
1      POLYGON ((-86.2958 35.5325, -86.58869321881345...
2      POLYGON ((-79.97289000000001 41.79664, -80.265...
3      POLYGON ((-82.56716 37.9724, -82.8600532188134...
4      POLYGON ((-83.88030000000001 36.77431, -84.173...
5      POLYGON ((-84.14578 36.63478, -84.438673218813...
6      POLYGON ((-84.22732999999999 36.73364, -84.520...
7      POLYGON ((-84.14578 36.63478, -84.438673218813...
8      POLYGON ((-86.70332999999999 34.58414, -86.996...
9      POLYGON ((-81.56874999999999 35.39114, -81.861...
10     POLYGON ((-86.2958 35.53244, -86.5886932188134...
11     POLYGON ((-82.8878 35.17769999911117, -83.1806...
12     POLYGON ((-81.78440000000001 40.3032, -82.0772...
13     POLYGON ((-81.1228 39.3295, -81.41569321881344...
14     POLYGON ((-82.9743 35.07779999911228, -83.2671...
15     POLYGON ((-81.98860000000001 36.97886, -82.281...
16     POLYGON ((-80.73990000000001 39.1112, -81.0327...
17     POLYGON ((-82.55835999999999 36.72267, -82.851...
18     POLYGON ((-82.92019999999999 35.1603, -83.2130...
19     POLYGON ((-82.65600000000001 36.64956, -82.948...
20     POLYGON ((-83.10619 37.0358, -83.3990832188134...
21     POLYGON ((-75.33848999999999 42.22403, -75.631...
22     POLYGON ((-81.75069999999999 39.2823, -82.0435...
23     POLYGON ((-80.1114 39.4714, -80.40429321881345...
24     POLYGON ((-80.5954 39.2699, -80.88829321881344...
25     POLYGON ((-80.1768 41.7243, -80.46969321881345...
26     POLYGON ((-86.536 35.463, -86.82889321881345 3...
27     POLYGON ((-85.3779 35.9417, -85.67079321881344...
28     POLYGON ((-85.94459999999999 36.0595, -86.2374...
29     POLYGON ((-86.10639999999999 36.1015, -86.3992...
30     POLYGON ((-80.4071 36.41299999909837, -80.6999...
                             ...                        
91     POLYGON ((-82.3044 40.8424, -82.59729321881345...
92     POLYGON ((-79.6212 37.3056, -79.91409321881345...
93     POLYGON ((-84.79362999999999 42.00977, -85.086...
94     POLYGON ((-93.62878000000001 37.12913, -93.921...
95     POLYGON ((-75.39342000000001 40.53344, -75.686...
96     POLYGON ((-93.61963 37.12537, -93.912523218813...
97     POLYGON ((-76.36111 40.62222, -76.654003218813...
98     POLYGON ((-83.6878 41.6929, -83.98069321881344...
99     POLYGON ((-76.152 40.4192, -76.44489321881345 ...
100    POLYGON ((-76.13139 40.42556, -76.424283218813...
101    POLYGON ((-84.95412 34.9871, -85.2470132188134...
102    POLYGON ((-74.87027999999999 41.56111, -75.163...
103    POLYGON ((-82.8454 39.9472, -83.13829321881344...
104    POLYGON ((-85.7533 39.8161, -86.04619321881344...
105    POLYGON ((-75.63642 40.0966, -75.9293132188134...
106    POLYGON ((-81.87869999999999 39.022, -82.17159...
107    POLYGON ((-76.095 40.42944, -76.38789321881345...
108    POLYGON ((-83.46854999999999 35.15883, -83.761...
109    POLYGON ((-82.1542 38.7264, -82.44709321881345...
110    POLYGON ((-82.0008 39.5547, -82.29369321881344...
111    POLYGON ((-79.6438 40.6148, -79.93669321881345...
112    POLYGON ((-85.25060000000001 34.77522, -85.543...
113    POLYGON ((-85.42753999999999 34.91407, -85.720...
114    POLYGON ((-82.89869 36.07914, -83.191583218813...
115    POLYGON ((-79.85899999999999 39.726, -80.15189...
116    POLYGON ((-79.6143 40.4283, -79.90719321881345...
117    POLYGON ((-80.9646 40.3281, -81.25749321881345...
118    POLYGON ((-85.30605 34.5821, -85.5989432188134...
119    POLYGON ((-81.8412 37.0393, -82.13409321881345...
120    POLYGON ((-83.46810000000001 35.16055, -83.760...
Name: geometry, dtype: object

In [443]:
geo_df.geometry


Out[443]:
1                 POINT (-87.2958 35.5325)
2      POINT (-80.97289000000001 41.79664)
3                POINT (-83.56716 37.9724)
4      POINT (-84.88030000000001 36.77431)
5               POINT (-85.14578 36.63478)
6      POINT (-85.22732999999999 36.73364)
7               POINT (-85.14578 36.63478)
8      POINT (-87.70332999999999 34.58414)
9      POINT (-82.56874999999999 35.39114)
10               POINT (-87.2958 35.53244)
11      POINT (-83.8878 35.17769999911117)
12      POINT (-82.78440000000001 40.3032)
13                POINT (-82.1228 39.3295)
14      POINT (-83.9743 35.07779999911228)
15     POINT (-82.98860000000001 36.97886)
16      POINT (-81.73990000000001 39.1112)
17     POINT (-83.55835999999999 36.72267)
18      POINT (-83.92019999999999 35.1603)
19     POINT (-83.65600000000001 36.64956)
20               POINT (-84.10619 37.0358)
21     POINT (-76.33848999999999 42.22403)
22      POINT (-82.75069999999999 39.2823)
23                POINT (-81.1114 39.4714)
24                POINT (-81.5954 39.2699)
25                POINT (-81.1768 41.7243)
26                  POINT (-87.536 35.463)
27                POINT (-86.3779 35.9417)
28      POINT (-86.94459999999999 36.0595)
29      POINT (-87.10639999999999 36.1015)
30      POINT (-81.4071 36.41299999909837)
                      ...                 
91                POINT (-83.3044 40.8424)
92                POINT (-80.6212 37.3056)
93     POINT (-85.79362999999999 42.00977)
94     POINT (-94.62878000000001 37.12913)
95     POINT (-76.39342000000001 40.53344)
96              POINT (-94.61963 37.12537)
97              POINT (-77.36111 40.62222)
98                POINT (-84.6878 41.6929)
99                 POINT (-77.152 40.4192)
100             POINT (-77.13139 40.42556)
101              POINT (-85.95412 34.9871)
102    POINT (-75.87027999999999 41.56111)
103               POINT (-83.8454 39.9472)
104               POINT (-86.7533 39.8161)
105              POINT (-76.63642 40.0966)
106      POINT (-82.87869999999999 39.022)
107               POINT (-77.095 40.42944)
108    POINT (-84.46854999999999 35.15883)
109               POINT (-83.1542 38.7264)
110               POINT (-83.0008 39.5547)
111               POINT (-80.6438 40.6148)
112    POINT (-86.25060000000001 34.77522)
113    POINT (-86.42753999999999 34.91407)
114             POINT (-83.89869 36.07914)
115      POINT (-80.85899999999999 39.726)
116               POINT (-80.6143 40.4283)
117               POINT (-81.9646 40.3281)
118              POINT (-86.30605 34.5821)
119               POINT (-82.8412 37.0393)
120    POINT (-84.46810000000001 35.16055)
Name: geometry, dtype: object

In [444]:
geo_df = geo_df.buffer(1).simplify(0.1, preserve_topology=False)

In [445]:
geo_df.geometry


Out[445]:
1      POLYGON ((-86.2958 35.5325, -86.58869321881345...
2      POLYGON ((-79.97289000000001 41.79664, -80.265...
3      POLYGON ((-82.56716 37.9724, -82.8600532188134...
4      POLYGON ((-83.88030000000001 36.77431, -84.173...
5      POLYGON ((-84.14578 36.63478, -84.438673218813...
6      POLYGON ((-84.22732999999999 36.73364, -84.520...
7      POLYGON ((-84.14578 36.63478, -84.438673218813...
8      POLYGON ((-86.70332999999999 34.58414, -86.996...
9      POLYGON ((-81.56874999999999 35.39114, -81.861...
10     POLYGON ((-86.2958 35.53244, -86.5886932188134...
11     POLYGON ((-82.8878 35.17769999911117, -83.1806...
12     POLYGON ((-81.78440000000001 40.3032, -82.0772...
13     POLYGON ((-81.1228 39.3295, -81.41569321881344...
14     POLYGON ((-82.9743 35.07779999911228, -83.2671...
15     POLYGON ((-81.98860000000001 36.97886, -82.281...
16     POLYGON ((-80.73990000000001 39.1112, -81.0327...
17     POLYGON ((-82.55835999999999 36.72267, -82.851...
18     POLYGON ((-82.92019999999999 35.1603, -83.2130...
19     POLYGON ((-82.65600000000001 36.64956, -82.948...
20     POLYGON ((-83.10619 37.0358, -83.3990832188134...
21     POLYGON ((-75.33848999999999 42.22403, -75.631...
22     POLYGON ((-81.75069999999999 39.2823, -82.0435...
23     POLYGON ((-80.1114 39.4714, -80.40429321881345...
24     POLYGON ((-80.5954 39.2699, -80.88829321881344...
25     POLYGON ((-80.1768 41.7243, -80.46969321881345...
26     POLYGON ((-86.536 35.463, -86.82889321881345 3...
27     POLYGON ((-85.3779 35.9417, -85.67079321881344...
28     POLYGON ((-85.94459999999999 36.0595, -86.2374...
29     POLYGON ((-86.10639999999999 36.1015, -86.3992...
30     POLYGON ((-80.4071 36.41299999909837, -80.6999...
                             ...                        
91     POLYGON ((-82.3044 40.8424, -82.59729321881345...
92     POLYGON ((-79.6212 37.3056, -79.91409321881345...
93     POLYGON ((-84.79362999999999 42.00977, -85.086...
94     POLYGON ((-93.62878000000001 37.12913, -93.921...
95     POLYGON ((-75.39342000000001 40.53344, -75.686...
96     POLYGON ((-93.61963 37.12537, -93.912523218813...
97     POLYGON ((-76.36111 40.62222, -76.654003218813...
98     POLYGON ((-83.6878 41.6929, -83.98069321881344...
99     POLYGON ((-76.152 40.4192, -76.44489321881345 ...
100    POLYGON ((-76.13139 40.42556, -76.424283218813...
101    POLYGON ((-84.95412 34.9871, -85.2470132188134...
102    POLYGON ((-74.87027999999999 41.56111, -75.163...
103    POLYGON ((-82.8454 39.9472, -83.13829321881344...
104    POLYGON ((-85.7533 39.8161, -86.04619321881344...
105    POLYGON ((-75.63642 40.0966, -75.9293132188134...
106    POLYGON ((-81.87869999999999 39.022, -82.17159...
107    POLYGON ((-76.095 40.42944, -76.38789321881345...
108    POLYGON ((-83.46854999999999 35.15883, -83.761...
109    POLYGON ((-82.1542 38.7264, -82.44709321881345...
110    POLYGON ((-82.0008 39.5547, -82.29369321881344...
111    POLYGON ((-79.6438 40.6148, -79.93669321881345...
112    POLYGON ((-85.25060000000001 34.77522, -85.543...
113    POLYGON ((-85.42753999999999 34.91407, -85.720...
114    POLYGON ((-82.89869 36.07914, -83.191583218813...
115    POLYGON ((-79.85899999999999 39.726, -80.15189...
116    POLYGON ((-79.6143 40.4283, -79.90719321881345...
117    POLYGON ((-80.9646 40.3281, -81.25749321881345...
118    POLYGON ((-85.30605 34.5821, -85.5989432188134...
119    POLYGON ((-81.8412 37.0393, -82.13409321881345...
120    POLYGON ((-83.46810000000001 35.16055, -83.760...
dtype: object

In [64]:
from geopandas import GeoSeries, GeoDataFrame
ajde = GeoDataFrame.from_file("/home/daniela/git/iSDM/data/urban_areas/ne_50m_urban_areas.shp")


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-64-a559129d60f8> in <module>()
      1 from geopandas import GeoSeries, GeoDataFrame
----> 2 ajde = GeoDataFrame.from_file("/home/daniela/git/iSDM/data/urban_areas/ne_50m_urban_areas.shp")

/home/daniela/anaconda/envs/biodiversity/lib/python3.5/site-packages/geopandas/geodataframe.py in from_file(cls, filename, **kwargs)
    173 
    174         """
--> 175         return geopandas.io.file.read_file(filename, **kwargs)
    176 
    177     @classmethod

/home/daniela/anaconda/envs/biodiversity/lib/python3.5/site-packages/geopandas/io/file.py in read_file(filename, **kwargs)
     26         else:
     27             f_filt = f
---> 28         gdf = GeoDataFrame.from_features(f, crs=crs)
     29 
     30     return gdf

/home/daniela/anaconda/envs/biodiversity/lib/python3.5/site-packages/geopandas/geodataframe.py in from_features(cls, features, crs)
    185         """
    186         rows = []
--> 187         for f in features:
    188             if hasattr(f, "__geo_interface__"):
    189                 f = f.__geo_interface__

fiona/ogrext.pyx in fiona.ogrext.Iterator.__next__ (fiona/ogrext.c:19067)()

fiona/ogrext.pyx in fiona.ogrext.FeatureBuilder.build (fiona/ogrext.c:3357)()

IndexError: list index out of range

In [63]:
ajde


Out[63]:
binomial category citation class_name compiler dist_comm family_nam genus_name geometry id_no ... presence seasonal shape_area shape_leng source species_na subpop subspecies tax_comm year
0 Acanthochelys pallidipectoris VU CRF REPTILIA Rhodin None CHELIDAE Acanthochelys POLYGON ((-59.20001220703125 -28.0374755859375... 75.0 ... 1.0 1.0 36.632836 54.770042 CBFTT pallidipectoris None None None 2013.0

1 rows × 26 columns


In [67]:
from geopandas import GeoDataFrame, GeoSeries

In [68]:
GeoDataFrame.from_file("../data/example.json")


Out[68]:
geometry
0 POINT (-105.01621 39.57422)

In [70]:
from osgeo import gdal
dem_file = gdal.Open("/home/daniela/git/iSDM/data/tmax1/tmax1.bil")

In [73]:
num_bands = dem_file.RasterCount
band = dem_file.GetRasterBand(1)
data = band.ReadAsArray()

In [74]:
data


Out[74]:
array([[-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       ..., 
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999]], dtype=int16)

In [75]:
band.GetNoDataValue()


Out[75]:
-9999.0

In [76]:
data.shape # degrees * 10, timezones? 8640/360=24


Out[76]:
(3600, 8640)

In [81]:
data[data>1].shape


Out[81]:
(4634609,)

In [86]:
from geopandas.geoseries import *

In [87]:
from geopandas import GeoDataFrame, GeoSeries

In [88]:
from shapely.geometry import Point, Polygon

In [121]:
p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)
p4 = Point(2.1,2.1)
p5 = Point(2,2.5)
p6 = Point(5,5)

In [122]:
points = GeoSeries([p1,p2,p3,p4,p5,p6])

In [127]:
poly = GeoSeries([Polygon([(0,0), (0,2), (2,2), (2,0)]),Polygon([(3,3), (3,6), (6,6), (6,3)])])

In [128]:
points.intersects(poly.unary_union)


Out[128]:
0     True
1     True
2     True
3    False
4    False
5     True
dtype: bool

In [129]:
points.plot()


Out[129]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a57f25dd8>

In [130]:
poly.plot()


Out[130]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3a57e45080>

In [120]:
type(poly)


Out[120]:
geopandas.geoseries.GeoSeries

In [12]:
from osgeo import ogr
from osgeo import gdal

geo = gdal.Open("/home/daniela/git/iSDM/data/tmax1/tmax12.bil")

In [13]:
drv= geo.GetDriver()

In [14]:
drv.GetMetadataItem('DMD_LONGNAME')


Out[14]:
'ESRI .hdr Labelled'

In [15]:
geo.GetLayerCount()


Out[15]:
0

In [16]:
band = geo.GetRasterBand(1)

In [17]:
type(band)


Out[17]:
osgeo.gdal.Band

In [18]:
band.ReadAsArray().shape


Out[18]:
(3600, 8640)

In [19]:
drv.LongName


Out[19]:
'ESRI .hdr Labelled'

In [20]:
drv.GetMetadataItem("MinX")

In [21]:
band.GetMetadataItem("MinX")

In [22]:
band.DataType


Out[22]:
3

In [23]:
geotransform = geo.GetGeoTransform()

In [24]:
geotransform


Out[24]:
(-180.00000000000335,
 0.041666666666667,
 0.0,
 90.00000000000003,
 0.0,
 -0.041666666666667)

In [25]:
import numpy as np
data = band.ReadAsArray(0, 0, geo.RasterXSize, geo.RasterYSize).astype(np.float)

In [26]:
band.GetNoDataValue()


Out[26]:
-9999.0

In [27]:
data[data>-9999]


Out[27]:
array([-240., -240., -240., ...,  177.,  161.,  177.])

In [28]:
geo1 = ogr.Open("/home/daniela/git/iSDM/data/tmax1/tmax12.hdr")

In [29]:
type(geo1)


Out[29]:
NoneType

In [30]:
data[data>-9999].min()/10


Out[30]:
-47.100000000000001

In [31]:
data[data>-9999].max()/10


Out[31]:
41.600000000000001

In [32]:
band.GetBlockSize()


Out[32]:
[8640, 1]

In [33]:
band.ComputeRasterMinMax()


Out[33]:
(-471.0, 416.0)

In [34]:
band.GetMinimum() # WTF?


Out[34]:
-447.0

In [35]:
band.GetMaximum() # WTF?


Out[35]:
407.0

In [36]:
band.GetRasterCategoryNames()

In [37]:
gdal.GetDataTypeName(band.DataType)


Out[37]:
'Int16'

In [38]:
band.GetMinimum()


Out[38]:
-447.0

In [39]:
drv.GetMetadata_List()


Out[39]:
['DCAP_CREATE=YES',
 'DCAP_CREATECOPY=YES',
 'DCAP_OPEN=YES',
 'DCAP_RASTER=YES',
 'DCAP_VIRTUALIO=YES',
 'DMD_CREATIONDATATYPES=Byte Int16 UInt16 Int32 UInt32 Float32',
 "DMD_CREATIONOPTIONLIST=<CreationOptionList>   <Option name='NBITS' type='int' description='Special pixel bits (1-7)'/>   <Option name='PIXELTYPE' type='string' description='By setting this to SIGNEDBYTE, a new Byte file can be forced to be written as signed byte'/></CreationOptionList>",
 'DMD_HELPTOPIC=frmt_various.html#EHdr',
 'DMD_LONGNAME=ESRI .hdr Labelled']

In [40]:
band.ComputeBandStats()


Out[40]:
(-7084.048300958076, 4560.980262032669)

In [41]:
band.ComputeStatistics(True)


Out[41]:
[-447.0, 407.0, 46.8697599022063, 220.4146774286982]

In [42]:
band.GetColorTable()

In [43]:
band.GetDataset()


Out[43]:
<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x7f8f70352180> >

In [44]:
import rasterio

In [45]:
with rasterio.open("/home/daniela/git/iSDM/data/tmax1/tmax12.bil") as src:
    print(src.width, src.height)
    print(src.crs)
    print(src.affine)
    print(src.count)
    print(src.indexes)


8640 3600
{'init': 'epsg:4326'}
| 0.04, 0.00,-180.00|
| 0.00,-0.04, 90.00|
| 0.00, 0.00, 1.00|
1
(1,)

In [46]:
dataset = rasterio.open("/home/daniela/git/iSDM/data/tmax1/tmax12.bil")

In [47]:
dataset.affine


Out[47]:
Affine(0.041666666666667, 0.0, -180.00000000000335,
       0.0, -0.041666666666667, 90.00000000000003)

In [48]:
dataset.colorinterp(1)


Out[48]:
<ColorInterp.undefined: 0>

In [49]:
dataset.meta


Out[49]:
{'affine': Affine(0.041666666666667, 0.0, -180.00000000000335,
       0.0, -0.041666666666667, 90.00000000000003),
 'count': 1,
 'crs': {'init': 'epsg:4326'},
 'driver': 'EHdr',
 'dtype': 'int16',
 'height': 3600,
 'nodata': -9999.0,
 'transform': (-180.00000000000335,
  0.041666666666667,
  0.0,
  90.00000000000003,
  0.0,
  -0.041666666666667),
 'width': 8640}

In [50]:
dataset.shape


Out[50]:
(3600, 8640)

In [51]:
type(dataset)


Out[51]:
rasterio._io.RasterReader

In [52]:
type(dataset.read())


Out[52]:
numpy.ndarray

In [53]:
type(band)


Out[53]:
osgeo.gdal.Band

In [54]:
type(data)


Out[54]:
numpy.ndarray

In [55]:
data.shape


Out[55]:
(3600, 8640)

In [56]:
dataset.read().shape


Out[56]:
(1, 3600, 8640)

In [57]:
dataset.read_band(1).shape


/home/daniela/anaconda/envs/biodiversity/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2885: FutureWarning: read_band() is deprecated and will be removed by Rasterio 1.0. Please use read() instead.
  exec(code_obj, self.user_global_ns, self.user_ns)
Out[57]:
(3600, 8640)

In [58]:
dataset.bounds  # YAY


Out[58]:
BoundingBox(left=-180.00000000000335, bottom=-60.000000000001165, right=179.9999999999995, top=90.00000000000003)

In [59]:
from rasterio.plot import show, show_hist

In [60]:
import rasterio.plot # my version is 0.25, they are up to 0.35. Maybe because I'm using python 3.5. Upgraded; works now

In [61]:
dataset.dtypes


Out[61]:
('int16',)

In [62]:
from rasterio.rio.insp import stats

In [127]:
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(18, 15))

ax.imshow(dataset.read(1), cmap="hot")


Out[127]:
<matplotlib.image.AxesImage at 0x7f8f44cf4978>

In [66]:
dataset.read(1)


Out[66]:
array([[-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       ..., 
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999],
       [-9999, -9999, -9999, ..., -9999, -9999, -9999]], dtype=int16)

In [78]:
my_data = dataset.read(1)

In [88]:
my_data[my_data>dataset.get_nodatavals()[0]]


Out[88]:
array([-240, -240, -240, ...,  177,  161,  177], dtype=int16)

In [83]:
my_data.shape


Out[83]:
(3600, 8640)

In [87]:
dataset.get_nodatavals()[0]


Out[87]:
-9999.0

In [92]:
my_data[2000,5000]/10


Out[92]:
34.299999999999997

In [95]:
from rasterio.transform import Affine
def transform_from_corner(ulx, uly, dx, dy):
    return Affine.translation(ulx, uly)*Affine.scale(dx, -dy)

print(transform_from_corner(bounds[0], bounds[3], 1.0/3600, 1.0/3600).to_gdal())


(119.52, 0.0002777777777777778, 0.0, -20.5, 0.0, -0.0002777777777777778)

In [96]:
from rasterio.features import rasterize
from shapely.geometry import Polygon, mapping

# image transform
bounds = (119.52, -21.6, 120.90, -20.5)
transform = transform_from_corner(bounds[0], bounds[3], 1.0/3600, 1.0/3600)

# Make raster image, burn in vector data which lies completely inside the bounding box
poly = Polygon(((120, -21), (120.5, -21), (120.5, -21.2), (120, -21.2)))
output = rasterize([poly], transform=transform, out_shape=(3961, 4969))
print(output)


[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]]

In [126]:
plt.imshow(output, cmap="hot")


Out[126]:
<matplotlib.image.AxesImage at 0x7f8f44cee828>

In [125]:
import rasterio
from rasterio.features import rasterize
from rasterio.transform import IDENTITY
geometry = {
    'type': 'Polygon',
    'coordinates': [[(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]]}

geometry = Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]).buffer(3)
rows = cols = 100

with rasterio.drivers():
    result = rasterize([geometry], out_shape=(rows, cols))
    with rasterio.open(
            "test.tif",
            'w',
            driver='GTiff',
            width=cols,
            height=rows,
            count=1,
            dtype=np.uint8,
            nodata=0,
            crs={'init': "EPSG:4326"}) as out:
        out.write(result.astype(np.uint8), indexes=1)

out.close()
result = None
out = None


/home/daniela/anaconda/envs/biodiversity/lib/python3.5/site-packages/rasterio/__init__.py:193: UserWarning: Dataset has no geotransform set.  Default transform will be applied (Affine.identity())
  s.start()

In [121]:
Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]).buffer(3)


Out[121]:

In [ ]: