Skip to content

Instantly share code, notes, and snippets.

@andrewxhill
Last active April 28, 2017 17:29
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewxhill/acc28ed000a62de4d234 to your computer and use it in GitHub Desktop.
Save andrewxhill/acc28ed000a62de4d234 to your computer and use it in GitHub Desktop.
Steps used to make map of California rivers

Link to final map

calirivers

Also take a look at the directional coloration map

rainbow

Step 1

This was only possible due to the awesome work of vector-river-map by Nelson Minar

git clone https://github.com/NelsonMinar/vector-river-map.git
cd vector-river-map

Step 2

sh dataprep/downloadNhd.sh 

Import resulting shapefile into CartoDB

Step 3

convert PlusFlowlineVAA.dbf to CSV using https://gist.github.com/bertspaan/8220892

Import CSV to CartoDB

Step 4

Run,

select
    cast(nhdflowline.comid as integer) as comid,
    cast(gnis_id as integer) as gnis_id,
    gnis_name as name,
    substring(plusflowlinevaa.reachcode::text from 1 for 8) as huc8,
    cast(streamorde as smallint) as strahler,
    nhdflowline.the_geom
  from nhdflowline, plusflowlinevaa
  where nhdflowline.comid = plusflowlinevaa.comid
    and nhdflowline.ftype != 'Coastline'

Create Table from query using the GUI. Name it rivers.

Optional steps

The map renders fine without joining the rivers. In fact, CartoDB uses the spatial index to only render rivers in view, so performance is actually likely better in most cases if you do not join river segments. If you want to join river segments follow these steps.

Step 5

Run,

select
  MAX(gnis_id) as gnis_id,
  MAX(name) as name,
  MAX(strahler) as strahler,
  MIN(huc8) as huc8,
  ST_LineMerge(ST_Union(the_geom)) as the_geom
from 
  rivers
where 
  gnis_id IS NOT NULL
  group by strahler

Create Table from query using the GUI. Name it merged_rivers.

Step 6

Run,

insert into merged_rivers(gnis_id, name, strahler, huc8, the_geom)
select
    MAX(gnis_id) as gnis_id,
    MAX(name) as name,
    MAX(strahler),
    MAX(huc8) as huc8,
    ST_LineMerge(ST_Union(the_geom)) as the_geom
from rivers
where gnis_id is null
group by strahler

CartoCSS

I just did a quick and simple style for my map

/** choropleth visualization */
@lw: 0.3;
#rivers{
  line-color: #5CA2D1;
  line-width: 0.5;
  line-opacity: 1;
  line-cap: round;
}
#rivers [ strahler <= 7] {
  line-width: @lw *5;
  [zoom>10]{line-width: @lw * 10;}
  [zoom>11]{line-width: @lw * 30;}
  [zoom>13]{line-width: @lw * 90;}
}
#rivers [ strahler <= 6] {
  line-width: @lw * 4;
  [zoom>10]{line-width: @lw * 8;}
  [zoom>11]{line-width: @lw * 24;}
  [zoom>13]{line-width: @lw * 72;}
}
#rivers [ strahler <= 5] {
  line-width: @lw * 3;
  [zoom>10]{line-width: @lw * 6;}
  [zoom>11]{line-width: @lw * 18;}
  [zoom>13]{line-width: @lw * 54;}
}
#rivers [ strahler <= 4] {
  line-width: @lw * 2.5;
  [zoom>10]{line-width: @lw * 5;}
  [zoom>11]{line-width: @lw * 15;}
  [zoom>13]{line-width: @lw * 45;}
}
#rivers [ strahler <= 4] {
  line-width: @lw * 2;
  [zoom>10]{line-width: @lw * 4;}
  [zoom>11]{line-width: @lw * 12;}
  [zoom>13]{line-width: @lw * 36;}
}
#rivers [ strahler <= 3] {
  line-width: @lw *1.5;
  [zoom>10]{line-width: @lw * 3;}
  [zoom>11]{line-width: @lw * 9;}
  [zoom>13]{line-width: @lw * 27;}
}
#rivers [ strahler <= 2] {
  line-width: @lw;
  [zoom>10]{line-width: @lw * 2;}
  [zoom>11]{line-width: @lw * 6;}
  [zoom>13]{line-width: @lw * 18;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment