Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d3.geoBounds gives incorrect bounds for one of the polygons in world-atlas countries50, Fiji) #243

Open
jrus opened this issue Jun 22, 2021 · 1 comment

Comments

@jrus
Copy link
Contributor

jrus commented Jun 22, 2021

If I do:

const countries50 = (() => {
  const
    url = "https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json",
    raw = await fetch(url),
    topo = await raw.json();
  return topojson.feature(topo, topo.objects.countries);
})();

const fiji = countries50.features[170];
const polygon = ({ type: 'Polygon', coordinates: fiji.geometry.coordinates[15] })
const bounds = d3.geoBounds(polygon)

The result is [[-180,-16.541749856561054],[180,-16.43238163897263]]

I would instead expect the left longitude to be 179.something and the right longitude to be -179.something.

After conversion from topojson to geojson, the polygon in question has coordinates:

[
  [180,-16.54001385310727],
  [179.9855998559986,-16.541749856561054],
  [179.9855998559986,-16.522653818569424],
  [-180,-16.48966975294752],
  [-179.94239942399423,-16.441061656241544],
  [-179.89919899198992,-16.43238163897263],
  [-179.9279992799928,-16.479253732224805],
  [-179.9279992799928,-16.503557780577793],
  [-180,-16.54001385310727],
  [180,-16.54001385310727]
]
@jrus
Copy link
Contributor Author

jrus commented Jun 22, 2021

While we are at it, I haven’t investigated closely, but countries50’s Antarctica gets a bounding box that does not go all the way to the south pole.

Graphically, both of these issues result in:

Screen Shot 2021-06-22 at 12 05 09 PM

Edit:

uh....

Ring 0 of the polygon:

{
  var antarctica = JSON.parse(JSON.stringify(countries50.features[239].geometry));
  antarctica.coordinates = antarctica.coordinates.slice(2, 3);      // Polygon 2 of the MultiPolygon
  antarctica.coordinates[0] = antarctica.coordinates[0].slice(0,1); // Ring 0 of the Polygon
  return d3.geoBounds(antarctica);
}

-> [[-180,-90], [180,90]]

Ring 1 of the polygon:

{
  var antarctica = JSON.parse(JSON.stringify(countries50.features[239].geometry));
  antarctica.coordinates = antarctica.coordinates.slice(2, 3);      // Polygon 2 of the MultiPolygon
  antarctica.coordinates[0] = antarctica.coordinates[0].slice(1,2); // Ring 1 of the Polygon
  return d3.geoBounds(antarctica);
}

-> [[-180,-90], [180,-63.22635473573485]]

Rings 0 and 1 together:

{
  var antarctica = JSON.parse(JSON.stringify(countries50.features[239].geometry));
  antarctica.coordinates = antarctica.coordinates.slice(2, 3);      // Polygon 2 of the MultiPolygon
  antarctica.coordinates[0] = antarctica.coordinates[0];            // Both rings of the Polygon
  return d3.geoBounds(antarctica);
}

-> [[-180,-89.99900007577445], [180,-63.22635473573485]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant