Venue/Indoor Map using D3.js and Geojson

前端 未结 2 1674
后悔当初
后悔当初 2020-12-10 09:37

I have created geojson file which contains all the features of 1st floor of a shopping mall. I got that venue map projected using d3.js with different colors but only some p

相关标签:
2条回答
  • 2020-12-10 10:29

    D3's mapping projections are designed to transform 3D earth coordinates into 2D browser coordinates, so they are not that great at transforming local coordinates like the ones you've got. And as Amelia outlines your putting in coordinates that are outside of what's expected.

    You'd be better off doing one of two things; creating a geometry stream based on 2 linear scales as outlined in this google groups discussion; or using d3's path generators.

    To creating a 2D path generator is straightforward in d3 something like this will work:

    var shops = d3.svg.line()
        .interpolate("linear")
        .x(function(d) {
            return xScale(d.x);
        })
        .y(function(d) {
            return yScale(d.y);
        }) 
    

    The real trick here is accessing the 'right' part of your json object. If you look into the geojson structure you see that there is a geometry part as well as an properties part. You need to dig through to pull out the coordinates and then pass them to the pavement generator. In this case it would be:

    d.geometry.coordinates
    

    which would obviously need to be referenced correctly.

    Note that the method outlined here isn't going to work if you have complex geometries such as multi-polygons, you'll need to do quite a bit more work. If that's what you've got you'll want to create a custom geometry stream.

    Now putting all of that together here's a working example of you're json.

    0 讨论(0)
  • 2020-12-10 10:39

    It looks like the d3 mapping tools really fall apart if you try to use coordinates other than longitude and latitude.

    I tried creating a "null" projection that just returns the input values, but the negative numbers and numbers greater than 360 were still getting wrapped by d3 before passing to the projection function. That avoids the trig errors from the Mercator projection, and it creates interesting art, but not the floor plan you were hoping for:

    var projection = d3.geo.projection(function(λ, φ) {
      return [ λ, φ ];
    });
    

    http://fiddle.jshell.net/rR2hG/1/

    However, all is not lost. The second image in that example is created by just passing the array of coordinates as the points of <polygon> elements. I think that's closer to what you wanted. So you'll need to do a little more work to grab the points from the data file but you can definitely visualize them just as an array of coordinates.

        svg2.selectAll("polygon")
            .data(jsonData.features)
            .enter()
            .append("polygon")
            .attr("points", function(d){ return d3.merge(d.geometry.coordinates);})
            .attr("fill", function (d, i) {
                return color1(i);
            });
    

    The only other suggestion is to write a script to convert your geoJSON file to geographic units. They don't have to be actual latitude and longitude of a particular place (you could still have the map centered on a reference point of your choice), but the scale has to be in degrees not feet or meters or whatever you are using.

    0 讨论(0)
提交回复
热议问题