score:5
The first problem is with your data join:
vis.append("g")
.selectAll("path")
.data(json.coordinates)
.enter().append("path")
.attr("d", path);
This would mean you want one path element for each element in the json.coordinates
array. Since your test data is a polygon, that would mean one path element for the exterior ring, and then perhaps multiple other path elements for any interior holes, if your data has them. I expect you just want a single path element for the entire polygon.
The second problem is that you’re not passing valid GeoJSON to the d3.geo.path instance. Because the data in your data join is json.coordinates
, you’re just passing an array of coordinates directly to path
, when you need to pass a GeoJSON geometry object or a feature. (See the GeoJSON specification.)
Fortunately both of these problems are easy to fix by eliminating the data join and rendering the full polygon. To add just one path element, just call selection.append:
vis.append("path")
.datum(json)
.attr("d", path);
Your projection will probably need adjusting (translate and scale), too. You might find the project to bounding box example useful here.
score:1
Do you really need to do it with D3?
I would suggest to go with more map oriented libraries like:
Leaflet vector layer has support for GeoJSON and its size is quite compact.
Open Layers is also an option but it's size is quite big.
Here is an example how I have used Leaflet + GeoJSON to display suburb shape http://www.geolocation.ws/s/6798/en
Source: stackoverflow.com
Related Query
- Drawing map with d3js from openstreetmap geojson
- Resize projection in d3JS in only one axis with a geojson map
- Map created from geojson file just fills the entire canvas with the color of the path fill
- d3js world map with geoJSon
- drawing circle with radius specified in meters on a map
- Suitable library for combining with D3js , to allowing drawing to webgl (2D)
- Drawing already projected geoJSON map in d3.js
- Drawing voronoi diagram from a csv file with d3.js
- Drawing simple lines with d3js
- Looking for a world map with a specific projection in d3js
- d3.js: Drawing arcs between two points on map from file
- Geojson map with D3 only rendering a single path in a feature collection
- D3.js: Set fitExtent or fitSize (or center and scale programmatically) for map with multiple geoJSON files
- Display only a single state with counties from a full US counties map
- How to get projected path definition strings (not SVG elements) from a GeoJSON dataset with D3?
- Create map that combines data from two files with common column - but data doesn't match one to one
- d3js v5 + Topojson v3 Map with border rendering
- Problem with drawing an arc on a d3 canvas map
- D3js generate paths from GeoJSON
- d3js v5: Create a SVG with groups from a JSON file
- Bangalore geojson map with d3 js
- how to start drawing a line with a space between y-axis and it using d3js
- How can I scale my map to fit my svg size with d3 and geojson path data
- d3.js: map with concentric circles emanating from dots
- how to convert data selected from a postgres database to json or csv to use it with d3js lib?
- d3 line and points on map from geojson or csv data
- Using data from two separate geoJSON files in D3.js map visualization
- JSON to SVG plan drawing with D3js
- How to use drag behavior from d3js with leapjs
- Assigning __data__ from parent node to child with javascript & d3js
More Query from same tag
- c3.js how to place the y label inside the bar in a horizontal bar graph?
- D3: Unable to append the labels to example's circle version
- D3 svg components not resizable
- histogram bars width with scale applied
- "TypeError: r is null" when creating d3 pie chart
- d3 Axis Bug with Tiny Domain?
- Javascript adding dynamic a tag to multiple divs
- add country id from json to dorling map
- JSON call error with d3js
- append d3 text to existing html div
- How to add a click event or mouseover on dynamic elements ( enter/update/exit )?
- "Object is not a function" Polymer 3 wrapper
- d3.hsl fails on Safari
- Getting the lat and long of a click event
- Ordering and grouping in a complex MySQL query
- Adding event marks to a line chart
- Title Attribute not working for an SVG Rect on Safari
- trouble accessing max of nested object data for use in a domain in D3
- Why are both of these lines coming up the same color?
- Is querySelectorAll a good way to select elements in multiple charts?
- Highlight all content inside g tag, whenever mouse above said tag. D3
- d3 path is being drawn twice while attempting path tween
- Export data to csv using Entity Framework
- Dynamically Update Multiple Charts in D3
- Animated Path multi series line chart in D3.Js
- Multiple points of Gravity in d3.js
- Native d3.js methods to save / store / write data to a database?
- how to draw lines/edges in a force directed graph?
- Uncaught SyntaxError: Unexpected token . D3 Tooltips
- D3.js How to rotate Text on a path