Skip to content

Instantly share code, notes, and snippets.

@mkessy
Last active January 4, 2016 01:08
Show Gist options
  • Save mkessy/8545864 to your computer and use it in GitHub Desktop.
Save mkessy/8545864 to your computer and use it in GitHub Desktop.

Can't figure out how to properly scale vector to fit raster.

<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
html {
min-width: 1040px;
}
.body {
background-color: #fcfcfc;
font-family: "Lora", sans-serif;
margin: 1em auto 4em auto;
position: relative;
width: 960px;
}
.chart {
border: 1px solid gray;
}
path {
fill: none;
stroke: black;
stroke-width: 1px;
}
</style>
<body>
<div class="chart"></div>
<script>
var width = 960,
height = 500;
var projection = d3.geo.albers()
.center([0, 47.3])
.parallels([45.8, 49.0])
.rotate([100.48, 0])
.scale(9500) // how to calculate this to fit raster?
.translate([width/2, height/2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select(".chart").append("svg")
.attr({width: width, height: height});
d3.json("nd.json", function(error, nd) {
var northDakota = topojson.feature(nd, nd.objects.states);
svg.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#nd");
svg.append("image")
// .attr("clip-path", "url(#clip)")
.attr("xlink:href", "nd.jpg")
.attr("width", width)
.attr("height", height);
svg.append("path").datum(northDakota)
.attr("id", "nd")
.attr("d", path);
});
</script>
</body>
</html>
all: nd.jpg nd.json
nd.jpg: nd.tiff
convert nd.tiff nd.jpg
nd.json: nd.shp
topojson \
-p \
-o nd.json \
-- nd.shp
touch nd.json
nd.tiff: NE2_HR_LC_SR_W_DR.tif nd.shp
gdalwarp \
-r bilinear \
-s_srs EPSG:4326 \
-t_srs '+proj=aea +lat_1=45.8 +lat_2=49.0 +lat_0=47.43 +lon_0=-100.48 +x_0=0 +y_0=0' \
-te -276347.294932 180591.286872 303742.906211 -165373.349713 \
-ts 960 500 \
NE2_HR_LC_SR_W_DR.tif \
nd.tiff
touch nd.tiff
nd.shp: states.shp
ogr2ogr -s_srs states.prj \
-t_srs '+proj=aea +lat_1=45.8 +lat_2=49.0 +lat_0=47.43 +lon_0=-100.48 +x_0=0 +y_0=0' \
nd.shp states.shp
touch nd.shp
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment