A transboundary-water NGO is ranking countries by the length of major river mapped inside their borders, as the first cut of a shared-basin index. The previous analyst projected everything to Web Mercator, which made Russia's rivers half again as long as Brazil's for no hydrological reason. The brief is to cut every river at the borders and measure each piece on the ellipsoid, so the number means the same thing in Norway and in Nigeria.
Write a script that produces river kilometres per country.
The datasets are mounted where the script runs:
/data/countries.geojson — country and dependency polygons/data/rivers.geojson — 461 river and lake centrelines, MultiLineStringsgeopandas, shapely, pyproj, pandas and numpy are available. Assign the finished table to a variable named `result`: a GeoDataFrame with one row per country, carrying the country geometry and
iso_a3, nameriver_km — total length of centreline inside the country, km, measured geodesicallyCut each river at the country boundaries and measure the pieces with a geodesic length (pyproj.Geod.geometry_length on WGS 84). A river that crosses a border is credited to each side for the part on that side. Do not write a file — the runner reads result.
/data/countries.geojson — EPSG:4326; the geometry is in geometry (Polygon / MultiPolygon).
| column | type | meaning |
|---|---|---|
country_id | string | CO- plus the ISO 3166-1 alpha-3 code |
name | string | Short name |
name_long | string | Long name |
iso_a3 | string | ISO 3166-1 alpha-3; -99 where Natural Earth assigns none |
iso_a2 | string | ISO 3166-1 alpha-2; -99 where Natural Earth assigns none |
continent | string | Continent |
subregion | string | UN subregion |
pop_est | number | Population estimate (persons) |
pop_year | integer | Year of the estimate |
gdp_md | number | GDP, millions of US dollars (USD m) |
gdp_year | integer | Year of the GDP figure |
economy | string | Natural Earth economy class |
income_group | string | World Bank income group |
/data/rivers.geojson — EPSG:4326; the geometry is in geometry (MultiLineString).
| column | type | meaning |
|---|---|---|
river_id | string | River identifier, RV- and four digits |
name | string | Name as labelled |
name_en | string | English name |
featurecla | string | River | Lake Centerline |
scalerank | integer | Prominence, lower is more prominent |
Run executes the script in your browser and shows you its output and a preview. Submit executes it again on the server and grades what result holds.
These are where the teaching is. Read them twice.
Cutting a global line layer at boundaries and measuring on the ellipsoid is the method under river, road and pipeline statistics in every international dataset, and the projection shortcut is the most common way they go wrong.
Files: /data/countries.geojson, /data/rivers.geojson · assign result
6 scored, 0 informational
Cuts rivers at the country boundary
17%Measures length geodesically
17%One row per country
17%Carries iso_a3, name and river_km
8%Total river kilometres inside all countries
25%The country with the most river kilometres
17%