<jupyter_start><jupyter_text>Lesson2: Introduction to Geopandas- https://kodu.ut.ee/~kmoch/geopython2019/L2/geopandas-basics.html<jupyter_code>import geopandas as gpd

fp = "DAMSELFISH_distributions.shp"

data = gpd.read_file(fp)
type(data)
data.head()
# import matplotlib and make it show plots directly (inline) in Jupyter notebooks
import matplotlib.pyplot as plt
%matplotlib inline

data.plot()
# Create a output path for the data
out_file_path = "DAMSELFISH_distributions_SELECTION.shp"

# Select first 50 rows, this a the numpy/pandas syntax to ``slice`` parts out a dataframe or array, from position 0 until (excluding) 50
selection = data[0:50]

# Write those rows into a new Shapefile (the default output file format is Shapefile)
selection.to_file(out_file_path)
data['geometry'].head()
selection = data[0:5]
for index, row in selection.iterrows():
    poly_area = row['geometry'].area
    print("Polygon area at index {0} is: {1:.3f}".format(index, poly_area))
data['area'] = data.area
data['area'].head(2)
max_area = data['area'].max()
mean_area = data['area'].mean()

print("Max area: {:.2f}\nMean area: {:.2f}".format(round(max_area, 2), round(mean_area, 2)))
# Import necessary modules first
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, Polygon
import fiona

# Create an empty geopandas GeoDataFrame
newdata = gpd.GeoDataFrame()
newdata
newdata['geometry'] = None
newdata
coordinates = [(26.722117, 58.380184), (26.724853, 58.380676), (26.724961, 58.380518), (26.722372, 58.379933)]
    
poly = Polygon(coordinates)

poly
newdata.loc[0, 'geometry'] = poly

newdata

newdata.loc[0, 'Location'] = 'Tartu Townhall Square'

newdata

print(newdata.crs)
# Import specific function 'from_epsg' from fiona module
from fiona.crs import from_epsg


# Set the GeoDataFrame's coordinate system to WGS84
newdata.crs = from_epsg(4326)

# Let's see how the crs definition looks like
newdata.crs

newdata.plot()
# Determine the output path for the Shapefile
out_file = "raekoja_plats.shp"

# Write the data into that Shapefile
newdata.to_file(out_file)
data
# Group the data by column 'BINOMIAL'
grouped = data.groupby('BINOMIAL')

# Let's see what we got
grouped
# Iterate over the group object
for key, values in grouped:
    individual_fish = values
    print(key)
# Let's see what is the LAST item that we iterated
individual_fish
type(individual_fish)
print(key)
import os

# Determine outputpath
result_folder = "results"

# Create a new folder called 'Results' (if does not exist) to that folder using os.makedirs() function
if not os.path.exists(result_folder):
    os.makedirs(result_folder)

# Iterate over the
for key, values in grouped:
    # Format the filename (replace spaces with underscores)
    updated_key = key.replace(" ", "_")
    out_name = updated_key + ".shp"

    # Print some information for the user
    print( "Processing: {}".format(out_name) )

    # Create an output path, we join two folder names together without using slash or back-slash -> avoiding operating system differences
    outpath = os.path.join(result_folder, out_name)

    # Export the data
    values.to_file(outpath)<jupyter_output>Processing: Abudefduf_concolor.shp
Processing: Abudefduf_declivifrons.shp
Processing: Abudefduf_troschelii.shp
Processing: Amphiprion_sandaracinos.shp
Processing: Azurina_eupalama.shp
Processing: Azurina_hirundo.shp
Processing: Chromis_alpha.shp
Processing: Chromis_alta.shp
Processing: Chromis_atrilobata.shp
Processing: Chromis_crusma.shp
Processing: Chromis_cyanea.shp
Processing: Chromis_flavicauda.shp
Processing: Chromis_intercrusma.shp
Processing: Chromis_limbaughi.shp
Processing: Chromis_pembae.shp
Processing: Chromis_punctipinnis.shp
Processing: Chrysiptera_flavipinnis.shp
Processing: Hypsypops_rubicundus.shp
Processing: Microspathodon_bairdii.shp
Processing: Microspathodon_dorsalis.shp
Processing: Nexilosus_latifrons.shp
Processing: Stegastes_acapulcoensis.shp
Processing: Stegastes_arcifrons.shp
Processing: Stegastes_baldwini.shp
Processing: Stegastes_beebei.shp
Processing: Stegastes_flavilatus.shp
Processing: Stegastes_leucorus.shp
Processing: Stegastes_rectifraenum.shp
Processi[...]<jupyter_text>Lesson2: Map projections- https://kodu.ut.ee/~kmoch/geopython2019/L2/projections.html<jupyter_code>import geopandas as gpd

# Filepath to the Europe borders Shapefile
fp = "Europe_borders.shp"

# Read data
data = gpd.read_file(fp)
data.crs
data['geometry'].head()
# Let's take a copy of our layer
data_proj = data.copy()

# Reproject the geometries by replacing the values with projected ones
data_proj = data_proj.to_crs(epsg=3035)
data_proj['geometry'].head()
import matplotlib.pyplot as plt

%matplotlib inline


# Plot the WGS84
data.plot(facecolor='gray');

# Add title
plt.title("WGS84 CRS");

# Remove empty white space around the plot
plt.tight_layout()

# Plot the one with ETRS-LAEA projection
data_proj.plot(facecolor='blue');

# Add title
plt.title("ETRS Lambert Azimuthal Equal Area projection");

# Remove empty white space around the plot
plt.tight_layout()
# Ouput file path
out_fp = "Europe_borders_epsg3035.shp"

# Save to disk
data_proj.to_file(out_fp)
from shapely.geometry import Point

from fiona.crs import from_epsg


tartu_lon = 26.7290

tartu_lat = 58.3780
proj4_txt = '+proj=aeqd +lat_0={} +lon_0={} +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'.format(tartu_lat, tartu_lon)
proj4_txt
data_d = data.to_crs(proj4_txt)
data_d.plot(facecolor='purple');

plt.title("World Azimuthal Equidistant, centred on Tartu");

plt.tight_layout();
tartu = gpd.GeoSeries([Point(tartu_lon, tartu_lat)], crs=from_epsg(4326))
tartu = tartu.to_crs(proj4_txt)
print(tartu)
data_d['country_centroid'] = data_d.centroid
data_d.head(2)
def calculateDistance(row, dest_geom, src_col='geometry'):
    """
    Calculates the distance between a single Shapely Point geometry and a GeoDataFrame with Point geometries.

    Parameters
    ----------
    dest_geom : shapely.Point
        A single Shapely Point geometry to which the distances will be calculated to.
    src_col : str
        A name of the column that has the Shapely Point objects from where the distances will be calculated from.
    target_col : str
        A name of the target column where the result will be stored.
    """
    # Calculate the distances
    dist = row[src_col].distance(dest_geom)
    # Tranform into kilometers
    dist_km = dist/1000
    # Assign the distance to the original data
    return dist_km
tartu_geom = tartu.get(0)
print(tartu_geom)
data_d['new_dist'] = data_d.apply(calculateDistance, dest_geom=tartu_geom, src_col='country_centroid', axis=1)

data_d.head(20)
max_dist = data_d['new_dist'].max()

mean_dist = data_d['new_dist'].mean()

print("Maximum distance to Tartu is {:.2f} km, and the mean distance is {:.2f} km.".format(max_dist, mean_dist))

data = data.to_crs(from_epsg(3035))

tartu = gpd.GeoSeries([Point(tartu_lon, tartu_lat)], crs=from_epsg(4326))
tartu = tartu.to_crs(from_epsg(3035))
tartu_geom = tartu.get(0)

data['country_centroid'] = data.geometry.centroid

data['new_dist'] = data.apply(calculateDistance, dest_geom=tartu_geom, src_col='country_centroid', axis=1)

data.head(20)
max_dist = data['new_dist'].max()

mean_dist = data['new_dist'].mean()

print("Maximum distance to Tartu is {:.2f} km, and the mean distance is {:.2f} km.".format(max_dist, mean_dist))

print("with accurate centering: Maximum distance to Tartu is 3403.98 km, and the mean distance is 1688.60 km.")<jupyter_output>with accurate centering: Maximum distance to Tartu is 3403.98 km, and the mean distance is 1688.60 km.
{'fraction_non_alphanumeric': 0.10379713572460912, 'fraction_numerical': 0.02588358954145316, 'mean_word_length': 5.385906040268456, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '41349', 'license': 'MIT', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2707, 'n_words': 719, 'path': 'source/_static/data/L2/lesson2.ipynb', 'repo_name': 'LandscapeGeoinformatics/geopython-ut-2019-course', 'size': '7611'}