Compose Spanish Census GeoJSON & TopoJSON map

How to compose a GeoJSON and TopoJSON map from dbf and shp file.

Data Visualization

Overview

Public administrations usually provide maps and public records in shapefile formats.

Shapefile

The shapefile is a geospatial vector data format introduced in the early 90’s . Actually, it is a collection of three files:

Other files like .proj, .shp.xml, .sbn … may be included.

In this example we are going to use the Spanish census section shapefile (INE Cartografía digitalizada):

Despite being useful for many purposes, shapefiles are not the best option for human reading or web visualization. The main problem is the size of the .shp files:

ginopalazzo@MacBook-Air cartografia_censo2011_nacional $ ls -la
total 777048
        352 Feb 22 12:29 .
       1952 Feb 22 12:23 ..
   11220226 Mar 14  2014 SECC_CPV_E_01_R_INE.dbf
        404 Mar 14  2014 SECC_CPV_E_01_R_INE.prj
     363236 Mar 14  2014 SECC_CPV_E_01_R_INE.sbn
      10172 Mar 14  2014 SECC_CPV_E_01_R_INE.sbx
  109242236 Mar 14  2014 SECC_CPV_E_01_R_INE.shp
      19492 Mar 14  2014 SECC_CPV_E_01_R_INE.shp.xml
     287780 Mar 14  2014 SECC_CPV_E_01_R_INE.shx

109Mb of shapes and 11Mb of data is not the best way to represent a map in a browser.

GeoJSON & TopoJSON

GeoJSON, based on JSON, allows us to store collections of geometric data types (including their non-spatial attributes) in one central structure.

A notable extension of GeoJSON is TopoJSON, that encodes geospatial topology and provides smaller file sizes (up to 80% reduction).

  241037101 Feb 22 12:29 SECC_CPV_E_01_R_INE_geoJSON.json
   35648891 Feb 22 12:24 SECC_CPV_E_01_R_INE_topoJSon.json
   11220226 Mar 14  2014 SECC_CPV_E_01_R_INE.dbf
        404 Mar 14  2014 SECC_CPV_E_01_R_INE.prj
  109242236 Mar 14  2014 SECC_CPV_E_01_R_INE.shp
     287780 Mar 14  2014 SECC_CPV_E_01_R_INE.shx

How to convert shapefiles into json type of files?

Web browser

NodeJS & CL

Mike Bostock wrote wonderful long posts with neat examples: https://medium.com/@mbostock/command-line-cartography-part-1-897aa8f8ca2c

Python

There are several gist solutions with different python libraries: