OSM rivers to one main branch Python: Difference between revisions

From wikiluntti
Line 4: Line 4:
Finland map.png|The river map of Finland. The main river is plotted using larger linewidth than the tertiarys.  
Finland map.png|The river map of Finland. The main river is plotted using larger linewidth than the tertiarys.  
Rivers of finland.png|1. The first plot of all the rivers without modifications.  
Rivers of finland.png|1. The first plot of all the rivers without modifications.  
Kemijoki tributaries.png|2. The main branch and tributaries.
</gallery>
</gallery>



Revision as of 14:25, 27 April 2026

Introduction


OSM Geofabrik provides huge amount of GIS data. I need to extract one main branch of that all.

The problems are

  1. There are many rivers haring the same name
  2. The shp data breaks
  3. There are cycles (loops) and tributaries (branches)

1. Plot and check

The analysis of different coordinate systems is shown elsewhere.

import geopandas as gpd
import matplotlib.pyplot as plt

finland = gpd.read_file("fi.shp")
rivers = gpd.read_file("gis_osm_waterways_free_1.shp")

#Check coordinate system
print(finland.crs)
print(rivers.crs) #Says None.
rivers = rivers.set_crs(epsg=4326)
rivers = rivers.to_crs(finland.crs)

#Other coordinate system. Looks even better, more familiar
finland = finland.to_crs(epsg=3067)
rivers = rivers.to_crs(epsg=3067)

#rivers = gpd.clip(rivers, finland) # Clip rivers to Finland

# Plotting
fig, ax = plt.subplots(figsize=(6, 10))
finland.plot( ax=ax, color="#f0f0f0", edgecolor="#444444", linewidth=0.8 )
rivers.plot( ax=ax, color="#4aa3df", linewidth=0.3, alpha=0.7 )

ax.set_axis_off() # Remove axes
plt.title("Rivers of Finland", fontsize=14)
plt.show()

2. Plot Kemijoki and tributaries

3. Plot and check

4. Plot and check


5. Plot and check

6. Plot and check