Giter VIP home page Giter VIP logo

reverse-geojson's Introduction

reverse-geojson

Reverse geojson generated with mapshaper to use it with d3.

Using d3 to plot a geojson from an INEGI* or INE** map can be complicated. If a tool like mapshaper is used to convert the shape (.shp) documents to geojson, they will have the vertices in a different order than d3 requires to graph them correctly.

To use it, you have to import the library, and pass it a geojson to correct the order in which its coordinates are arranged(clockwise). It also supports an array of points.

// import the method
const reverseGeojson = require("reverse-geojson");

// reverse the geojson
const clockwiseCoordinates   = reverseGeojson(someGeojson);

For example, if we use mapshaper to generate a geojson of the Mexican Republic using the official INEGI shape file, and from there we use d3 to display it, this is what we would get:

import {create} from "d3-selection";
import { geoPath, geoMercator } from "d3-geo";
const someGeojson = require("./mexico.json");

const width  = 800;
const height = 800;

let root  = document.getElementById("mexico");
let svg   = create("svg").attr("viewBox", [0, 0, width, height]);

let projection = geoMercator().fitExtent([[0, 0], [width, height]], someGeojson);
let path       = geoPath(projection);

svg.selectAll("path")
    .data(someGeojson.features)
    .enter().append("path")
    .attr("d",path);

root.appendChild(svg.node())

error with reverse points

Instead, if we first change the order of the coordinates, the map will be displayed correctly:

import {create} from "d3-selection";
import { geoPath, geoMercator } from "d3-geo";
const reverseGeojson = require("reverse-geojson");
const someGeojson = require("./mexico.json");

const width  = 800;
const height = 800;
// reverse the coordinates first
const correctGeojson = reverseGeojson(someGeojson);

let root  = document.getElementById("mexico");
let svg   = create("svg").attr("viewBox", [0, 0, width, height]);

let projection = geoMercator().fitExtent([[0, 0], [width, height]], correctGeojson);
let path       = geoPath(projection);

svg.selectAll("path")
    .data(correctGeojson.features)
    .enter().append("path")
    .attr("d",path);

root.appendChild(svg.node())

correct render of bananas' republic

The function is based on this response on stackoverflow

* Instituto Nacional de Estadística, Geografía e Informática (México)

** Instituto Nacional Electoral (México)

reverse-geojson's People

Contributors

elcoruco avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.