Error in projecting D3 world-50m.json file causing fill issues

I am currently working with the world-50m.json file for a projection, but I'm facing an issue where several countries are being cut off at the edges when filled with color. This results in horizontal fill sections/lines across the map.

This problem is also visible in the d3-geo example projection available here: https://github.com/d3/d3-geo/blob/master/test/data/world-50m.json

https://i.sstatic.net/DrVSD.jpg

I'm wondering if there is another JSON file that doesn't have these cutoff countries or if I could somehow exclude specific polygons from my fill. Identifying all the countries/shapes with this issue might be challenging, especially because some of them are small and possibly not essential to the map (except for Russia).

Here is the code snippet I'm using:

var w = 960,
    h = 660,
    active = d3.select(null);

var projection = d3.geoMercator()
    .scale(150)
    .translate([w/2, h/2])
    .precision(.1);

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

var countries = svg.append("svg:g").attr("id", "countries");

d3.json("world-50m.json", function(error, us) {
  mapFeatures = topojson.feature(us, us.objects.countries).features;
  mapFeatures.type = "countries";
  drawMap();
});

function drawMap() {
  countries.selectAll("path")
        .data(mapFeatures)
        .enter().append("svg:path")
        .attr("d", path)
        .attr("class", "feature")
        .attr("data-id", function(d) { return d.id; })
        .style("fill", "blue")
        .style("stroke", "white")
        .style("stroke-width", ".2px");
};

Any assistance on resolving this issue would be highly appreciated!

Answer №1

The resolution to this issue can be found in the comments above, thanks to the assistance of @Andrew Reid and @altocumulus.

The problem observed was related to Antimeridian Cutting, which d3 was not handling correctly due to differences in path and projection calls between versions d3 v3 and d3 v4 syntax.

To address this issue, the solution involved modifying geo.path() to geoPath(). This adjustment resolved the discrepancy and allowed d3 to display the map properly while supporting antimeridian cutting.

Here is the updated code snippet:

var w = 960,
    h = 660,
    active = d3.select(null);

var projection = d3.geoMercator()
    .scale(150)
    .translate([w/2, h/2])
    .precision(.1);

var path = d3.geoPath()
    .projection(projection);

var countries = svg.append("svg:g").attr("id", "countries");

d3.json("world-50m.json", function(error, us) {
  mapFeatures = topojson.feature(us, us.objects.countries).features;
  mapFeatures.type = "countries";
  drawMap();
});

function drawMap() {
  countries.selectAll("path")
        .data(mapFeatures)
        .enter().append("svg:path")
        .attr("d", path)
        .attr("class", "feature")
        .attr("data-id", function(d) { return d.id; })
        .style("fill", "blue")
        .style("stroke", "white")
        .style("stroke-width", ".2px");
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Monitor Firebase information: trigger a function once the document object model has been refreshed

Currently, I am in the process of developing a real-time chat application using Vue.js and Firebase's real-time database. One specific functionality I am attempting to implement is ensuring that the chat window automatically scrolls to the bottom whe ...

Stripping /r /n tags from TinyMCE content stored in MySQL

Whenever I retrieve content from the database and display it on the screen, I notice that my output contains /r/n tags. Even though I managed to remove the slashes while handling other tags using: htmlspecialchars_decode(stripslashes($row[Content])) The ...

Unable to retrieve field values from Firestore if they are numeric rather than strings

I need to display this information in a list format: li.setAttribute('data-id', updatesArgument.id);//'id' here unique id Example 1 name.textContent = updatesArgument.data().count;// Works if String Example 2 let i=1; nam ...

How do I initiate PUT and DELETE requests from my HTML code?

I am currently in the process of developing a web application that will manage items within a list. Previously, I used buttons with event listeners and JavaScript functions to handle editing and deleting items. However, I am now transitioning towards build ...

"Learn how to dynamically update the value of a text box using a loop in jQuery when the

I am looking for a way to update the value of the second input field when the first one is changed. For example, if the user changes the value in "valuex00", I want the same value to be displayed in "valuey00". This should apply to all corresponding pairs ...

Trigger the show.bs.modal event prior to the parent event being called

In my Bootstrap website (built with Bootstrap 4), I have a button that triggers a modal and transfers the data from the button to the form inside the modal using the following function: $('#exampleModal').on('show.bs.modal', function ( ...

What is the method for excluding POJO annotations when utilizing the Jackson ObjectMapper?

I have a POJO which includes Jackson annotations. public class Sample{ private String property1; @JsonIgnore private String property2; //...setters getters } When using the Jackson library for automarshalling ...

Transform the json structure into a different json layout

I am struggling with converting a JSON file from one format to another as I am new to JSON. Here is the initial JSON structure that needs to be converted: { "message": "Successfully Advisor data has been fetched.", "success": true, "data": { ...

Is it possible to integrate external search functionality with Vuetify's data table component

I am currently working with the v-data-table component from Vuetify, which comes with a default filter bar in its properties. This table retrieves data from a local JSON file. The issue arises when I have another external component, a search bar created u ...

Changes made to Objective C variables within an Async block do not affect the value of the Class variable

I am facing an issue with using my parsed JSON values from an async block in different methods and in the viewDidLoad function. In the async block, it returns the value properly, but in other methods, it just returns null. Below is a snippet of code from ...

PostgreSQL Function starts successfully but then encounters an error

This particular function... CREATE OR REPLACE FUNCTION public.find_locations( search_text character varying, record_offset integer DEFAULT 0, fetch_quantity integer DEFAULT 20, sort_by character varying DEFAULT 'name', directi ...

Leveraging Discord.js to retrieve all messages sent while the bot was inactive

My plan is to create a bot that will store all messages sent in a channel into a txt file on my computer. However, the challenge is that since my computer is not always on when the bot is running, there are gaps in the stored messages in the .txt file. I a ...

The term 'EmployeeContext' is being utilized as a namespace in this scenario, although it actually pertains to a type.ts(2702)

<EmployeeContext.Provider> value={addEmployee, DefaultData, sortedEmployees, deleteEmployee, updateEmployee} {props.children}; </EmployeeContext.Provider> I am currently facing an issue mentioned in the title. Could anyone lend a hand? ...

Access and retrieve pkpass files from a server using React

I've exhausted all options but still can't get it to work. I'm attempting to create an Apple wallet pass using https://github.com/walletpass/pass-js. When I download the pass on the node server where I've implemented it, everything work ...

`Unable to retrieve Shopify products using shopify-api-node`

I'm encountering a 403 error while trying to retrieve products from my custom Shopify app using the shopify-api-node module. Below is the code snippet I've put together based on a helpful discussion on Stackoverflow: const express = require(& ...

Is there a way to identify which elements are currently within the visible viewport?

I have come across solutions on how to determine if a specific element is within the viewport, but I am interested in knowing which elements are currently visible in the viewport among all elements. One approach would be to iterate through all DOM elements ...

Aligning text vertically to the top with material UI and the TextField component

Seeking guidance on adjusting vertical alignment of text in <TextField /> const styles = theme => ({ height: { height: '20rem', }, }); class Foo extends React.component { ... <TextField InputProps={{ classes: ...

What is the best way to retrieve information from a Node.js web service using JavaScript?

I am currently using a nodejs web service URL to perform authentication by making AJAX calls. However, I would like to achieve the same functionality using simple JavaScript without AJAX. Below is my service.js code: var LOGIN_AUTHENTICATION = "http://19 ...

Obtain a value from a JsonArray and save it in a separate array

Does anyone know how to extract the "score" values from this ArrayofJson dataset? I am looking to store scores 1, 2, 1, 3 in an array like int sample[] = {1, 2, 1, 3}; import org.json.JSONArray; import org.json.JSONObject; p ...

How to Resolve jQuery Script Delay Caused by AJAX Request?

I am in the process of creating a unique responsive Wordpress Theme, with only one page that loads content via AJAX. Here is the AJAX function I have implemented: jQuery(document).ready(function(){ jQuery.ajaxSetup({cache:false}); jQuery(" ...