import geopandas as gpd
# read Shapely file
data = gpd.read_file('data/DAMSELFISH_distributions.shp')
# read JSON file
temp = gpd.read_file('selection.json')
data.head()
ID_NO | BINOMIAL | ORIGIN | COMPILER | YEAR | CITATION | SOURCE | DIST_COMM | ISLAND | SUBSPECIES | ... | RL_UPDATE | KINGDOM_NA | PHYLUM_NAM | CLASS_NAME | ORDER_NAME | FAMILY_NAM | GENUS_NAME | SPECIES_NA | CATEGORY | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 183963.0 | Stegastes leucorus | 1 | IUCN | 2010 | International Union for Conservation of Nature... | None | None | None | None | ... | 2012.1 | ANIMALIA | CHORDATA | ACTINOPTERYGII | PERCIFORMES | POMACENTRIDAE | Stegastes | leucorus | VU | POLYGON ((-115.64375 29.71392, -115.61585 29.6... |
1 | 183963.0 | Stegastes leucorus | 1 | IUCN | 2010 | International Union for Conservation of Nature... | None | None | None | None | ... | 2012.1 | ANIMALIA | CHORDATA | ACTINOPTERYGII | PERCIFORMES | POMACENTRIDAE | Stegastes | leucorus | VU | POLYGON ((-105.58995 21.89340, -105.56483 21.8... |
2 | 183963.0 | Stegastes leucorus | 1 | IUCN | 2010 | International Union for Conservation of Nature... | None | None | None | None | ... | 2012.1 | ANIMALIA | CHORDATA | ACTINOPTERYGII | PERCIFORMES | POMACENTRIDAE | Stegastes | leucorus | VU | POLYGON ((-111.15962 19.01536, -111.15948 18.9... |
3 | 183793.0 | Chromis intercrusma | 1 | IUCN | 2010 | International Union for Conservation of Nature... | None | None | None | None | ... | 2012.1 | ANIMALIA | CHORDATA | ACTINOPTERYGII | PERCIFORMES | POMACENTRIDAE | Chromis | intercrusma | LC | POLYGON ((-80.86500 -0.77894, -80.75930 -0.833... |
4 | 183793.0 | Chromis intercrusma | 1 | IUCN | 2010 | International Union for Conservation of Nature... | None | None | None | None | ... | 2012.1 | ANIMALIA | CHORDATA | ACTINOPTERYGII | PERCIFORMES | POMACENTRIDAE | Chromis | intercrusma | LC | POLYGON ((-67.33922 -55.67610, -67.33755 -55.6... |
5 rows × 24 columns
fig = data.plot()
import pandas as pd
panda_data = pd.DataFrame(data) # Pandas DataFrame
selection = data[0:50]
# write .cpg, .dbf, .prj, .shp, .shx
selection.to_file('shapely.shp')
# write .json
selection.to_file('selection.json', driver="GeoJSON")
poly = data['geometry'][0] # Polygon
xy = poly.exterior.coords.xy # array tuple
xs = xy[0].tolist() # x coordinates
ys = xy[0].tolist() # y coordinates
selection = data[0:5]
for index, row in selection.iterrows():
print(index, row['geometry'].area)
0 19.39625403004423 1 6.145902112999523 2 2.6972072716440176 3 87.46062072709621 4 0.0009183696153124292
from shapely.geometry import Point, Polygon
import fiona
newdata = gpd.GeoDataFrame()
coordinates = [(24.950899, 60.169158), (24.953492, 60.169158), (24.953510, 60.170104), (24.950958, 60.169990)]
poly = Polygon(coordinates)
newdata['geometry'] = None
newdata['Location'] = None
newdata.loc[0, 'geometry'] = poly
newdata.loc[0, 'Location'] = 'Senaatintori'
newdata
geometry | Location | |
---|---|---|
0 | POLYGON ((24.95090 60.16916, 24.95349 60.16916... | Senaatintori |
grouped = data.groupby('BINOMIAL')
for key, values in grouped:
print(key, values.shape, type(values))
Abudefduf concolor (6, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Abudefduf declivifrons (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Abudefduf troschelii (10, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Amphiprion sandaracinos (51, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Azurina eupalama (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Azurina hirundo (5, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis alpha (14, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis alta (11, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis atrilobata (9, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis crusma (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis cyanea (15, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis flavicauda (2, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis intercrusma (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis limbaughi (7, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis pembae (4, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chromis punctipinnis (1, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Chrysiptera flavipinnis (5, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Hypsypops rubicundus (4, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Microspathodon bairdii (10, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Microspathodon dorsalis (9, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Nexilosus latifrons (7, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes acapulcoensis (9, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes arcifrons (8, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes baldwini (1, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes beebei (8, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes flavilatus (8, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes leucorus (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes rectifraenum (3, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Stegastes redemptus (2, 24) <class 'geopandas.geodataframe.GeoDataFrame'> Teixeirichthys jordani (7, 24) <class 'geopandas.geodataframe.GeoDataFrame'>
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
print(world.crs)
ax = world.plot()
epsg:4326
world.head()
pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
---|---|---|---|---|---|---|
0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... |
1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... |
4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... |
world = world.to_crs("EPSG:3395")
ax = world.plot()
world.head() # the coordinates have been changed
pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
---|---|---|---|---|---|---|
0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((20037508.343 -1800679.237, 200... |
1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((3774143.866 -105050.440, 3792946.708... |
2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-964649.018 3185897.152, -964597.245... |
3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-13674486.249 6242596.000, -13... |
4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-13674486.249 6242596.000, -13... |
data = gpd.read_file('Europe_borders/Europe_borders.shp') # EPSG:4326
data_proj = data.copy()
data_proj = data_proj.to_crs(epsg=3035) # EPSG:3035
data.plot()
<AxesSubplot:>
data_proj.plot()
<AxesSubplot:>
data.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
data_proj.crs
<Projected CRS: EPSG:3035> Name: ETRS89-extended / LAEA Europe Axis Info [cartesian]: - Y[north]: Northing (metre) - X[east]: Easting (metre) Area of Use: - name: Europe - European Union (EU) countries and candidates. Europe - onshore and offshore: Albania; Andorra; Austria; Belgium; Bosnia and Herzegovina; Bulgaria; Croatia; Cyprus; Czechia; Denmark; Estonia; Faroe Islands; Finland; France; Germany; Gibraltar; Greece; Hungary; Iceland; Ireland; Italy; Kosovo; Latvia; Liechtenstein; Lithuania; Luxembourg; Malta; Monaco; Montenegro; Netherlands; North Macedonia; Norway including Svalbard and Jan Mayen; Poland; Portugal including Madeira and Azores; Romania; San Marino; Serbia; Slovakia; Slovenia; Spain including Canary Islands; Sweden; Switzerland; Turkey; United Kingdom (UK) including Channel Islands and Isle of Man; Vatican City State. - bounds: (-35.58, 24.6, 44.83, 84.17) Coordinate Operation: - name: Europe Equal Area 2001 - method: Lambert Azimuthal Equal Area Datum: European Terrestrial Reference System 1989 - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich