Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting points on a map with D3

I'm trying to plot a few points onto a map using the D3 geo library based on latitudes and longitudes. However, when I pass these values into my projection function, it results in coordinates that our outside the bounds of my SVG image. My code is based on this example provided in the documentation.

I've thrown the current code up at: http://bl.ocks.org/rpowelll/8312317

My source data is a simple array of objects formatted like so

var places = [
  {
    name: "Wollongong, Australia",
    location: {
      latitude: -34.42507,
      longitude: 150.89315
    }
  },
  {
    name: "Newcastle, Australia",
    location: {
      latitude: -32.92669,
      longitude: 151.77892
    }
  }
]

Following this I set up an Plate Carrée projection like so:

var width = 960,
height = 480

var projection = d3.geo.equirectangular()
    .scale(153)
    .translate([width / 2, height / 2])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection)

From there I draw the map with code effectively identical to the linked example. At the end of my script, I use the following code to plot points on this map:

svg.selectAll(".pin")
    .data(places)
  .enter().append("circle", ".pin")
    .attr("r", 5)
    .attr("transform", function(d) {
      return "translate(" + projection([
        d.location.latitude,
        d.location.longitude
      ]) + ")"
    })

However this code results in points that are outside of the SVG element's bounds. Is there anything obvious I'm doing wrong here?

like image 801
rpowell Avatar asked Jan 08 '14 05:01

rpowell


People also ask

What is d3 Geo projection?

# d3.geo.projection(raw) Constructs a new projection from the specified raw point projection function. For example, a Mercator projection can be implemented as: var mercator = d3. geo.

What does d3 map do?

Use d3. map to group items together, creating a hashed array that can be accessed using handy functions like array. get(value) .

What is the correct syntax to draw a circle in d3?

var circle = d3. selectAll("circle"); With a selection, we can make various changes to selected elements. For example, we might change the fill color using selection.


1 Answers

You have a simple typo in your code -- coordinates should be passed as (longitude, latitude) to the projection, not the other way round. This code should work fine:

 svg.selectAll(".pin")
  .data(places)
  .enter().append("circle", ".pin")
  .attr("r", 5)
  .attr("transform", function(d) {
    return "translate(" + projection([
      d.location.longitude,
      d.location.latitude
    ]) + ")";
  });
like image 194
Lars Kotthoff Avatar answered Oct 13 '22 01:10

Lars Kotthoff