Skip to content
Projects

sebastian-meier/d3.geo2rect has 204 GitHub stars and 9 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-18 23:05 UTC. Last push 2019-01-07.

How these counts are measured

Measured star growth

in 24h
0
in 7 days
Collecting history
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

About

Morphing geojson polygons into rectangles

Developer Tools

README.md

Original

d3.geo2rect

Morphing geojson polygons into rectangles

DemoClip

The package has two modules: compute (function) and draw (class). For using this one needs to include the d3 and turf.js library.

compute

Compute for morphing a geojson into rectangles we need to clean the geojson first. All MultiPolygons will be transformed into Polygons (largest polygon will be used). Holes will be removed. The original data is stored in .geometry.ocoordinates. Afterwards a second set of coordinates is being generated representing a rectangle. The rectangle has the bounding box 0,0|1,1 so its easy to transform. In addition each coordinate has its centroid in the geo-space stored within. The rectangle coordinates are stored in .geometry.qcoordinates.

d3.json('./data/de.geojson', function(err, data){
  var geojson = geo2rect.compute(data);
});

draw

Draw takes care of drawing the data received from the compute function.

Simply create a new draw instance:

var g2r = new geo2rect.draw();

Send the configs:

var config = {
  width : 700,
  height : 700,
  padding : 70,
  projection : d3.geoMercator(),
  duration : 1000,
  key:function(d){return d.properties.Kurz; },
  grid : {
    SH:{x:1,y:0},
    HB:{x:0,y:1},
    HH:{x:1,y:1},
    MV:{x:2,y:1},
    NI:{x:0,y:2},
    BB:{x:1,y:2},
    BE:{x:2,y:2},
    NW:{x:0,y:3},
    ST:{x:1,y:3},
    SN:{x:2,y:3},
    RP:{x:0,y:4},
    HE:{x:1,y:4},
    TH:{x:2,y:4},
    SL:{x:0,y:5},
    BW:{x:1,y:5},
    BY:{x:2,y:5}
  }
};
g2r.config = config;

Please note, that this script does not compute the grid layout. See nmap and nmap-squared for examples of how to do this automatically.

Then send the computed data from the compute function:

g2r.data = geojson;

An d3 svg object:

g2r.svg = svg.append('g');

And then draw the whole thing:

g2r.draw();

You can simply switch the mode by calling:

g2r.toggle();

The current mode can be checked via:

g2r.mode

To update and draw again, simply call the draw function again:

g2r.draw();

Examples

In the examples folder you find three examples for the states of Germany and the US as well as London boroughs.

Live examples can be found here:

Germany US London Italy