1

I'm learning how to use projection with d3, so I was following FreeCodeCamp (from now on FCC) lessons and I stumbled upon a problem with the expected output from two different files.

I based my test on this project from Mike Bostock.

When I use the json file from this project everything work as expected, if I use one of the file from a FCC challenge I get a strange pattern.

This is the working code,

const width = 960;
const height = 500;

const projection = d3.geoAlbersUsa()
                     .scale(1300)
                     .translate([487.5,305]);
const path = d3.geoPath(projection);

const svg = d3.select("body")
             .append("svg")
             .attr("width", width)
             .attr("height", height);

d3.json("https://cdn.jsdelivr.net/npm/us-atlas@3/counties-10m.json")
   .then(data => {

      svg.selectAll("path")
        .data(topojson.feature(data, data.objects.states).features)
        .join("path")
        .attr("fill", "none")
        .attr("stroke", "red")
        .attr("d", path)
        .attr('class', 'county');

}).catch(err => console.log(err));

When I use this json file the result is a mess. I'm trying to figure out what are the difference and how to tell when I need to use different source files.

AskaNor_29
  • 97
  • 1
  • 12
  • 1
    Your second json uses a different coordinate system than the first - the first is unprojected (spherical coordinates with lat long), the second is projected (coordinates on a plane, probably in pixel values). You are projecting the already projected data as if it was unprojected, see [here](https://stackoverflow.com/a/42430876/7106086). – Andrew Reid Sep 19 '19 at 16:31
  • @AndrewReid How can I tell the coordinate system when I don't know the source? There is a way to grasp the type only by looking? Or I have to log the result of topojson.feature(data, data.objects.states).features to try and understand the type? – AskaNor_29 Sep 19 '19 at 16:59
  • 1
    Converting to geojson (by using topojson.feature) is a way to confirm your coordinates are latitude/longitude or unprojected (as geojson shows the coordinates of each vertice, you should be able to see if the coordinates exceed +/- 90 units north/south and +/- 180 units west/east) - but if it is projected, unless there is a description of the projection used to create the file, determining the projection (and thereby the coordinate system) is very difficult and sometimes impossible. Shapefiles include a prj file which can be useful, unfortunately, geojson/topojson usually lack metadata. – Andrew Reid Sep 19 '19 at 17:03
  • Thank you! It was driving me crazy! – AskaNor_29 Sep 19 '19 at 17:34

0 Answers0