TopoJSON conversion leading to surprising shapes and figures not found in original JSON

I am trying to showcase the topoJSON on a leaflet map using d3. To achieve this, I am following an example on GitHub available at this link. Here is the code I have extracted from the GitHub example:

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8">
    <title>D3 Test</title>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">
    <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
    <script type="text/javascript" src="http://d3js.org/topojson.v1.min.js"></script>
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
</head>

<body>
    <div id="map" style="width:600px; height:600px;"></div>
    <script>
            var map = new L.Map("map", {
                            center: [37.8, -96.9],
                            zoom: 4
                    })
                    .addLayer(new L.TileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"));

            var svg = d3.select(map.getPanes().overlayPane).append('svg');
            var g = svg.append('g').attr('class', 'leaflet-zoom-hide');

            d3.json("pathToTopotJson", function(error, collection) {
                    if (error) throw error;
                    console.log(collection)

                    var bounds = d3.geo.bounds(topojson.feature(collection, collection.objects.collection));
                    var path = d3.geo.path().projection(projectPoint);

                    var feature = g.selectAll('path')
                            .data(topojson.feature(collection, collection.objects.collection).features)
                            .enter()
                            .append('path');


                    map.on('viewreset', reset);
                    reset();

                    // Reposition the SVG to cover the features.
                    function reset() {
                            var bottomLeft = projectPoint(bounds[0]),
                                    topRight = projectPoint(bounds[1]);

                            svg.attr('width', topRight[0] - bottomLeft[0])
                                    .attr('height', bottomLeft[1] - topRight[1])
                                    .style('margin-left', bottomLeft[0] + 'px')
                                    .style('margin-top', topRight[1] + 'px');

                            var translation = -bottomLeft[0] + ',' + -topRight[1];
                            g.attr('transform', 'translate(' + -bottomLeft[0] + ',' + -topRight[1] + ')');

                            feature.attr('d', path);
                    }
                    // Use Leaflet to implement a D3 geographic projection.
                    function projectPoint(x) {
                            var point = map.latLngToLayerPoint(new L.LatLng(x[1], x[0]));
                            return [point.x, point.y];
                    }

            })
    </script>

The topoJSON is being displayed, but with unexpected Polygons/Lines: https://i.sstatic.net/7KoIW.png

When I display the JSON, the polygons/lines are not visible: https://i.sstatic.net/rHpim.png

What could be causing this issue with the topoJSON? Is it an error in my code or during the conversion process?

Answer №1

What's the issue with topoJSON? Is it an error in my code or did the conversion process fail?

There is no error per se, this is a known issue that occurs when a polygon intersects the antimeridian.

To avoid this issue, consider reprojecting your data to a different map projection or utilize the --spherical and --cartesian topojson command-line options as a workaround.

It's recommended to research antimeridian crossings and isolate problematic geometries, such as Russia, to check if transforming only that geometry to TopoJSON resolves the display issue. Isolating problematic geometries can simplify troubleshooting and highlight any bugs more effectively.

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

Is there a way to prevent the context menu from appearing when tapping on an image on a mobile phone?

When attempting to move an image on the screen with a touch event, a popup appears in Chrome when pressing and holding the image. This popup usually includes options to save the image. Is there a way to disable this popup so I can freely move the image w ...

What is the functionality of Mongoose for handling multiple updates?

Array; arr=[ { id: [ '5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af' ], color: [ 'a', 'b' ] } ] Models; const varyant = Models.varyant function; Promise.all( arr.map((item)=>{ return var ...

Having trouble importing my function component into a router in reactjs. Need some guidance on how to properly set it up

I am working on a Slider component function called export default Slider. In my App.js file, I have the following code: function App() { return ( <Router> <Routes> <Route exact path='/' element={<Home />} /> ...

Developing a Customized Styling Guide Using Node.JS and User Specifications

Seeking a method to generate a personalized user style sheet utilizing input from users. Users can choose options like background colors, and the app will generate a custom style sheet based on their selections. Their input will be captured through HTML in ...

Can you provide insight on the recommended methodology for aggregating JSON data effectively?

Imagine having a dataset structured like this: let sampleData = [ {"YEAR": "2009", "MONTH": "1", "CUSTOMER": "Customer1", "REVENUE": "1938.49488391425"}, {"YEAR": "2009", "MONTH": "1", "CUSTOMER": "Customer2", "REVENUE": "75.9142774343491"}, {"YEAR": "200 ...

What causes a React Ref callback to be invoked multiple times during the initial loading of a page?

To find more information, please visit this URL within the React DOCS. You can also access a version of this code here. I acknowledge that it is recommended to use the useCallback hook in a Functional React Component to create a ref callback according to ...

Adjusting the format of a JavaScript object

Looking at the object below: {A: 52, B: 33} I want to transform it into this format: ["A", 52], ["B", 33] Any recommendations on how to achieve this conversion? ...

Determine the horizontal movement of x and z on a flat surface while accounting for

Using HammerJS, I am able to manipulate a 3D object within an augmented reality environment. Everything functions properly unless I physically move my phone (which serves as the camera)... const newTranslation = new THREE.Vector3(this._initTranslation.x ...

Obtaining the image with PHP

As a newcomer to PHP and Ajax queries, I am trying to utilize the croppie.js plugin to upload a profile image. However, when I upload the image, it is saved as 1580192100.png. The problem arises when attempting to later retrieve the image based on the user ...

Prevent Rails from attempting to generate a template error with ActionView::MissingTemplate

Currently, I am working on setting up a basic Angular Rails application. Below is the code for my rails controller: class ItemsController < ApplicationController respond_to :json, :html def index @items = Item.order(params[:sort]).page(params ...

Oh no! "The accuracy of your BMI calculation is in question."

I am currently working on a technical assessment for a BMI calculator, but I am facing a challenge in implementing the formula. The instructions for calculating BMI are as follows: Step 1: The user's height is given in feet, so it needs to be conver ...

What is the best way to vertically center a button against a video using CSS?

How can I vertically center a button against a video using CSS? Here is my code: https://jsbin.com/curefoyefe/edit?html,css,js,output <video controls="" ng-show="myForm_live_video.live_video.$valid" ngf-src="live_video" width="200" height="200" class= ...

Cipher/decipher URL Redirect Parameter

While sending the return URL as a query string parameter, it looks like this: http://localhost:50316/TripNestFinal/Login.aspx?ReturnUrl=~/Account/AccountSettings.aspx However, I am seeking a way to encode ~/Account/AccountSettings.aspx in a manner that wi ...

Can you identify the issue with my database file?

Here is the content from my database.js file: const MongoClient = require('mongodb').MongoClient; const db = function(){ return MongoClient.connect('mongodb://localhost:27017/users', (err, database) => { if (err) return co ...

Enhancing data binding in Angular 2.0 with string interpolation

In my script, I am working with a string that goes like this: {name} is my name. Greeting {sender} Is there a module available in Angular 2.0 that allows me to use something similar to the string.format() function in C#? I understand that it can be achie ...

The submission feature for the textarea in Javascript is not functioning properly

As someone who is new to frontend development, I am currently facing a challenge with debugging JavaScript code that involves making changes to the content of a textarea. Despite my efforts to debug using the browser console, I have yet to identify why it ...

The CloudWatch logs for a JavaScript Lambda function reveal that its handler is failing to load functions that are defined in external

Hello there, AWS Lambda (JavaScript/TypeScript) is here. I have developed a Lambda handler that performs certain functions when invoked. Let me walk you through the details: import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda' ...

Creating a stylish CSS button with split colors that run horizontally

Could you please provide some guidance on creating a button design similar to this one? I've made progress with the code shown below, but still need to make adjustments like changing the font. <!DOCTYPE html> <html> <head> <sty ...

What is the process for retrieving data from mongoDB and displaying it by year in a single row?

I have received an array of data from MongoDB, which includes id, userName, and date. The date consists of years and months. My goal is to display this data categorized by years. How can I construct a query to the database that will show the data for all y ...

What are some effective techniques for handling asynchronous operations while utilizing [displayWith] in an autocomplete

In my angular reactive form, I am struggling with an autocomplete functionality. I want to show the name (myObject.name) but use the ID (myObject.id) as the value. However, when the form is populated with existing values, there is a delay in retrieving th ...