Created
July 30, 2015 16:55
-
-
Save chronick/c3f3377a5999780b66e0 to your computer and use it in GitHub Desktop.
Render arbitrary geoJSON on a div with d3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_setupGeometry: -> | |
map = @props.map | |
map.drawing.disable() | |
map.hideClus() | |
map.hideControls() | |
width = 500 | |
height = 500 | |
geometry = _.cloneDeep(@props.geometry) | |
MapUtils.forceRHR(geometry[0]) | |
# // Create a unit projection. | |
projection = d3.geo.mercator() | |
.scale(1) | |
.translate([0, 0]) | |
# // Create a path generator. | |
path = d3.geo.path() | |
.projection(projection) | |
# // Compute the bounds of a feature of interest, then derive scale & translate. | |
b = path.bounds(geometry[0]) | |
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height) | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2] | |
# // Update the projection to use computed scale & translate. | |
projection | |
.scale(s) | |
.translate(t) | |
d3.select(@refs.pathWrapper.getDOMNode()) | |
.append('svg').style | |
'width': "500px" | |
'height': "500px" | |
.append('path') | |
.data(geometry) | |
.attr('d', path) | |
.attr({ | |
'fill-opacity': 0.2 | |
'stroke-width': 1.5 | |
'stroke-opacity': 0.8 | |
'stroke': 'white' | |
'fill': 'white' | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment