• Stars
    star
    335
  • Rank 125,904 (Top 3 %)
  • Language
    C
  • License
    BSD 2-Clause "Sim...
  • Created over 11 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Indexes points and lines and generates map tiles to display them

Datamaps

This is a tool for indexing large lists of geographic points or lines and dynamically generating map tiles from the index for display.

Dependencies

  • Modern C compiler like gcc or clang
  • make
  • libpng
  • Ideally a 64 bit machine with >8 GB free memory

Installation

First install make and libpng then type:

make

After the build finishes you will have 4 new command line programs available in the local directory:

encode render enumerate merge

Usage

The basic idea is that if you have a file of points like this:

40.711017,-74.011017
40.710933,-74.011250
40.710867,-74.011400
40.710783,-74.011483
40.710650,-74.011500
40.710517,-74.011483

or segments like this:

40.694033,-73.987300 40.693883,-73.987083
40.693883,-73.987083 40.693633,-73.987000
40.693633,-73.987000 40.718117,-73.988217
40.718117,-73.988217 40.717967,-73.988250
40.717967,-73.988250 40.717883,-73.988433
40.717883,-73.988433 40.717767,-73.988550

you can index them by doing

cat file | ./encode -o directoryname -z 16

to encode them into a sorted quadtree in web Mercator in a new directory named directoryname, with enough bits to address individual pixels at zoom level 16.

You can then do

./render -d directoryname 10 301 385 

to dump back out the points that are part of that tile, or

./render directoryname 10 301 385 > foo.png

to make a PNG-format map tile of the data. (You need more data if you want your tile to have more than just one pixel on it though.)

Alternately, if you want an image for a particular area of the earth instead of just one tile, you can do

./render -A -- directoryname zoom minlat minlon maxlat maxlon > foo.png

The "--" is because otherwise getopt will complain about negative numbers in the latitudes or longitudes. For example you could use

./render -A -- dots.dm 12 37.192596 -122.811526 38.070528 -121.702961 > sf.png

to generate an image of the San Francisco Bay Area at zoom level 12 from the encoded data in dots.dm.

The point indexing is inspired by Brandon Martin-Anderson's Census Dotmap. The vector indexing is along similar lines but uses a hierarchy of files for vectors that fit in different zoom levels, and I don't know if anybody else does it that way.

Rendering assumes it can mmap an entire copy of the file into the process address space, which isn't going to work for large files on 32-bit machines. Performance, especially at low zoom levels, will be much better if the file actually fits in memory instead of having to be swapped in.

Merging files

encode will only write to a brand new file. If you want to add data to an existing file, the way to do it is to create a new file with encode and then use merge to combine the old and the new.

$ cat newdata | encode -o new.dm
$ merge -o combined.dm old.dm new.dm

merge also has an option, -u, to eliminate duplicates between the source files while merging them.

Generating a tileset

The enumerate and render programs work together to generate a tileset for whatever area there is data for. If you do, for example,

$ enumerate -z14 dirname | xargs -L1 -P8 ./render -o tiles/dirname

enumerate will output a list of all the zoom/x/y combinations that appear in dirname through zoom 14, and xargs will invoke render on each of these to generate the tiles into tiles/dirname.

You can enumerate a single zoom by specifying both -z and -Z for maximum and minimum. So if you want just z12, enumerate -z12 -Z12.

The -P8 makes xargs invoke 8 instances of render at a time. If you have a different number of CPU cores, a different number may work out better.

If you want to filter the output of render, for example through pngquant to reduce the number of colors, you can do it by having xargs invoke a subshell.

$ enumerate -z8 dirname | xargs -L1 -P8 sh -c 'mkdir -p tiles/dirname/$2/$3; render $1 $2 $3 $4 | pngquant 32 > tiles/dirname/$2/$3/$4.png' dummy

The dummy argument is important because sh -c eats the first argument after the command.

Adding color to data

The syntax for color is kind of silly, but it works, so I had better document it.

Colors are denoted by distance around the color wheel. The brightness and saturation are part of the density rendering; the color only controls the hue.

If you want to have 256 possible hues, that takes 8 bits to encode, so you need to say

encode -m8

to give space in each record for 8 bits of metadata. Each input record, in addition to the location, also then needs to specify what color it should be, and the format for that looks like

40.711017,-74.011017 :0
40.710933,-74.011250 :85
40.710867,-74.011400 :170

to make the first one red, the second one green, and the third one blue. And then when rendering, you do

render -C256

to say that it should use the metadata as 256ths of the color wheel.


Options to render

Input file, zoom level, and bounds

The basic form is

render dir zoom x y

to render the specified tile into a PNG file on the standard output.

-A ... dir zoom minlat minlon maxlat maxlon
Instead of rendering a single tile (zoom/x/y), the invocation format changes to render the specified bounding box as a single image.
-f dir
Also read input from dir in addition to the file in the main arguments. You can use this several times to specify several input files.

Output file format

-d
Output plain text (same format as encode uses) giving the coordinates and metadata for each point or line within the tile.
-D
Output GeoJSON giving the coordinates and metadata for each point or line within the tile.
-T pixels
Image tiles are pixels pixels on a side. The default is 256. 512 is useful for high-res "retina" displays.
-r
Leaflet-style retina, where a request for a tile at zoom level N is actually a request for a quarter of a tile at zoom level N-1. In this case, the quarter-tiles remain 256x256.
-o dir
Instead of outputting the PNG image to the standard output, write it in a file in the directory dir in the zoom/x/y hierarchy. It will also write a basic dir/metadata.json that will be used if you package the tiles with mbutil.

Background

-t opacity
Changes the background opacity. The default is 255, fully opaque.
-w
The default background color becomes white, not black.
-b hex
Specifies hex to be the background color. The default is black (or white, if -w is set.)
-m
Makes the output image a mask: The data areas are transparent and the background is opaque. The default is the opposite.

Color

-c hex
Specifies hex to be the fully saturated color at the middle of the output range. The default is gray.
-S hex
Specifies hex to be the oversaturated color at the end of the output range. The default is white.
-s
Use only the color range leading up to full saturation. The default treats saturated color as the middle of the range and allows the output to be oversaturated all the way to white (or the -S color).

Brightness and thickness

-B base:brightness:ramp
Sets the basic display parameters:
  • Base is the zoom level where each point is a single pixel. The default is 13.
  • Brightness is the value contributed by each dot at that zoom level. The default is 0.05917. With the default (square root) gamma, this means it takes 4 dots on the same pixel to reach full color saturation and 16 to reach full oversaturation. (It should have been 0.0625 so that it would hit it exactly.)
  • Ramp is the an additional brightness boost given to each dot as zoom levels get higher, or taken away as zoom levels get lower, slightly increasing the effect of halving the number of dots with each zoom level. The default is 1.23.
-e exponent
Allows specifying a different rate at which dots are dropped at lower zoom levels. The default is 2, and anything much higher than that will look terrible at low zoom levels, and anything much lower will be very slow at low zoom levels. 1.5 seems to work pretty well for giving a quality boost to the low zoom levels. The ramp from -B is automatically adjusted to compensate for the change.
-G gamma
Sets the gamma curve, which causes each additional dot plotted on the same pixel to have diminishing returns on the total brightness. The default is 0.5, for square root.
-L thickness
Sets the base thickness of lines. The default is 1, for a single pixel thickness.
-l ramp
Sets the thickness ramp for lines. The line gets thicker by a factor of ramp for each zoom level beyond the base level from -B. The default is 1, for constant thickness. Thicker lines are drawn dimmer so that the overall brightness remains the same.
-p area
Specifies a multiplier for dot sizes. Point brightness is automatically reduced by the same factor so the total brightness remains constant, just diffused. The default is 1. (Example -p5 for area 5)
-p garea
Specifies a Gaussian brush instead of a flat disk, as well as a multiplier for dot sizes. (Example: -pg5 for Gaussian with area 5)

Metadata

-C hues
Interpret the metadata as one of hues hues around the color wheel. Numbering starts at 0 for red and continues through orange, yellow, green, blue, violet, and back to red.
-C meta1:hue1:meta2:hue2
Specify a range of hues that correspond to a domain of meta values. The hues are numbered in degrees: 0 for red, 30 for orange, 60 for yellow, 120 for green, 180 for cyan, 240 for blue, 300 for violet. You can specify hues below 0 or above 360 to wrap around across red.
-x cradiusf / -x cradiusm
Interpret the metadata as a number of points to be plotted in the specified radius (in feet or meters) around the point in the data.
-x b
Make the brightness of each feature proportional to the metadata value.
-x r
Make the radius of each point proportional to the metadata value.
-x smax
Cap the saturation of meta colors at max instead of 0.7. They will go all the way to white if you use 1.
-x u
Use an approximation of CIELCH uniform color space so that all hues with the same density will have approximately equal lightness and saturation. Blues will be brighter and greens will be dimmer.

Compensation

-g
Reduce the brightness of lines whose endpoints are far apart, to compensate for GPS samples that jump around, or bogus connections to to 0,0.
-O base:dist:ramp
Tune the parameters for reasonable distances between points:
  • Base is the zoom level at which only fully acceptable samples are given full brightness. The default is 16.
  • Dist is the allowable distance between samples at the base zoom level. The unit is z32 tiles, or about 1cm, and I need to make that something more human-oriented. The default is 1600.
  • Ramp is the factor of additional distance that is allowed at each lower zoom level. The default is 1.5.
-M latitude
Mercator compensation. Makes the dots bigger at latitudes higher than the one specified and smaller at latitudes closer to the equator.

Vector styling

-v
Instead of the normal output, produce a CartoCSS file for TileMill 2 to approximate the brightness, dot ramp, gamma, and colors you specified. Use render-vector to make the vector tiles themselves.

Useless

-a
Turn off anti-aliasing

More Repositories

1

if-then-else

!!Con West 2019 talk
349
star
2

housing-inventory

San Francisco housing construction history and associated data
Shell
134
star
3

geotools

Tools for working with geographic data
C
87
star
4

tile-stitch

Stitch together and crop map tiles for a specified bounding box
C
87
star
5

ascii

History of ASCII and its predecessors
PostScript
31
star
6

twitter-oauth

Tiny program to make the Authorization header for the Twitter streaming API
C
19
star
7

json-pull

Streaming pull parser for JSON in C
C
14
star
8

twitter-json

The mess I am using to parse Twitter JSON
Java
13
star
9

geotaggers

The Geotaggers' World Atlas
CartoCSS
12
star
10

osm-tiger-update

A tool for updating OpenStreetMap with changes that have been made to the US Census TIGER maps since 2006.
C
10
star
11

osm-animate

Animation of OSM mapping activity
C
8
star
12

junix

Unix as if JSON mattered
C
7
star
13

ed

Ed is the standard text editor
C
7
star
14

gpx-layer

Tools to turn GPX files into a GPS map tracing layer
Perl
7
star
15

la-traffic-counts

Extracting Los Angeles's traffic counts from published PDF files
HTML
6
star
16

apple2-converters

Programs I wrote many years ago to convert Apple II binary file formats to things that are usable on current systems
C
4
star
17

cat

Implementations of cat(1) in different languages
JavaScript
4
star
18

colorwheel

Make a key for the datamaps color wheel
JavaScript
4
star
19

ridership-model

Predicting BART ridership from LEHD origin-destination pairs
Perl
4
star
20

snap-carriageways

Try to snap GPS logs to directional carriageways without the help of a base map
C
4
star
21

v6man

Unix Sixth Edition manual
Roff
3
star
22

unixio

Asynchronous buffered I/O for Node in the Unix style
JavaScript
3
star
23

srtm-elevation

Making an image out of the SRTM digital elevation model
C
3
star
24

high-injury

Chopping roads up into blocks in the manner of SFMTA's high injury streets analysis
Python
3
star
25

lzss

Standard and readable implementations of LZSS compression
C
3
star
26

daily-traffic-counts

Daily pedestrian/bike/vehicle counts and correlations with other data sets
Perl
3
star
27

gpx-import

Fork of http://git.openstreetmap.org/gpx-import.git/
C
3
star
28

tlid-ways

Mapping from TIGER TLIDs to OpenStreetMap ways
3
star
29

trafficways

HTML code that I used on trafficways.org
JavaScript
3
star
30

learning-to-program

How I learned to program
C
3
star
31

trump-tweets

Archive of Trump's old tweets
HTML
2
star
32

ttyedit

Code from 1999 Usenix paper "New Tricks for an Old Terminal Driver"
C
2
star
33

cart

Cart cartogram tool from http://www-personal.umich.edu/~mejn/cart/
C
2
star
34

mkpw

Generate line noise passwords
C
2
star
35

vector-subtract

C
2
star
36

macbinary

Extract the data forks from old Macintosh MacBinary II (.bin) files
C
2
star
37

srtm-gridded-vector

Make vector tiles of SRTM elevation data
C++
2
star
38

posix-multibyte-tests

Things that might go wrong with multibyte characters in Unix shell tools
Shell
2
star
39

tippecanoe-protomaps

protomaps fork of tippecanoe - build big vector tilesets of thematic data
C++
2
star
40

vancouver-traffic-counts

Extracting Vancouver's traffic counts from the published HTML files
HTML
2
star
41

fantasy-transit

Making fantasy transit maps
C++
2
star
42

map-overlay

The society for putting maps on top of other maps
JavaScript
2
star
43

osm-snap

Flatten ways from OpenStreetMap XML to datamaps format
C
1
star
44

tide

Perl
1
star
45

bresenham-snap-rounding

C++
1
star
46

jstr

If <string.h> did JSON
C
1
star
47

chromium-compact-language-detector

Fork of an old revision of https://code.google.com/p/chromium-compact-language-detector/
C++
1
star
48

triangle-cartogram

Trying to flatten triangular meshes
C++
1
star
49

coreutils

Fork of https://git.savannah.gnu.org/git/coreutils.git. New work is in the "multibyte-squash" branch
C
1
star
50

midi-key-guesser

Guess the key of a piece of music from its MIDI file
C++
1
star
51

indent

Patch to GNU indent
C
1
star
52

ebola

Recent edits
Perl
1
star
53

gnu-coding-standards

Revisions of the GNU Coding Standards from the 1990s
1
star
54

see-something

The code that I used to make the "See something or say something" maps
C
1
star
55

hilbert

Draw a Hilbert curve
C
1
star
56

wispy

Wispy Maps
C
1
star
57

bart-station-profile-study

Trips to BART station by mode extracted from the 2008 Station Profile Study
Perl
1
star
58

twitter-usenet

Turning tweets into Usenet articles
JavaScript
1
star
59

multidimensional-scaling

Terrible, terrible code for multidimensional scaling. Please don't try to use this mess.
JavaScript
1
star
60

pedestrian-volume-model

Perl
1
star
61

sf-traffic-counts

Traffic counts from San Francisco Municipal Transportation Agency
Perl
1
star
62

dlib-face

Command-line tools for dlib's face recognition library
C++
1
star
63

la-subway-ridership

Los Angeles subway ridership by station
Perl
1
star
64

read-speed

How fast can you read data from different types of media?
C
1
star
65

check-one-way

Check GPS traces against OSM ways to find the ones with wrong one-way tagging
Perl
1
star
66

tiger-test

US Census TIGER as GeoJSON
1
star
67

nyc-traffic-counts

Traffic counts from New York City
Perl
1
star