Skip to content

Instantly share code, notes, and snippets.

@rveciana
Last active May 15, 2019 15:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rveciana/fe6b452c853146e674dd6dd09c1cc6e3 to your computer and use it in GitHub Desktop.
Save rveciana/fe6b452c853146e674dd6dd09c1cc6e3 to your computer and use it in GitHub Desktop.
Congressional representatives dataset

Based on this data, contains a Shapefile and two topojson with the US congressional Districts.

The atlas-make project has a better maintained version of this data.

The difference from the data is based on, is that the American Samoa, Guam and Northern Marianas are included from the Natural Earth dataset. The Congress Representative data is included too.

get_areas.py is a script to calculate the area paoperties for the new objects. The name of the Congressional Representatives in the added territories has been taken from the Wikipedia.

The topojson has been calculated using the command: topojson -o us_congressional_district.json cgd114p010g.shp -p

The topojson data can be used with d3 and the d3-composite-projections library, as seen in the example.

// http://geoexamples.com/d3-composite-projections/ Version 1.0.0. Copyright 2016 Roger Veciana i Rovira.
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3-geo"),require("d3-path")):"function"==typeof define&&define.amd?define(["exports","d3-geo","d3-path"],e):e(n.d3=n.d3||{},n.d3,n.d3)}(this,function(n,e,t){"use strict";function o(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function r(){function n(n){var e=n[0],t=n[1];return c=null,i.point(e,t),c||(l.point(e,t),c)||(s.point(e,t),c)}var r,a,i,l,s,c,p=e.geoAlbers(),u=e.geoConicEqualArea().rotate([154,0]).center([-2,58.5]).parallels([55,65]),f=e.geoConicEqualArea().rotate([157,0]).center([-3,19.9]).parallels([8,18]),m={point:function(n,e){c=[n,e]}};return n.invert=function(n){var e=p.scale(),t=p.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.12&&r<.234&&o>=-.425&&o<-.214?u:r>=.166&&r<.234&&o>=-.214&&o<-.115?f:p).invert(n)},n.stream=function(n){return r&&a===n?r:r=o([p.stream(a=n),u.stream(n),f.stream(n)])},n.precision=function(e){return arguments.length?(p.precision(e),u.precision(e),f.precision(e),n):p.precision()},n.scale=function(e){return arguments.length?(p.scale(e),u.scale(.35*e),f.scale(e),n.translate(p.translate())):p.scale()},n.translate=function(e){if(!arguments.length)return p.translate();var t=p.scale(),o=+e[0],r=+e[1];return i=p.translate(e).clipExtent([[o-.455*t,r-.238*t],[o+.455*t,r+.238*t]]).stream(m),l=u.translate([o-.307*t,r+.201*t]).clipExtent([[o-.425*t+x,r+.12*t+x],[o-.214*t-x,r+.234*t-x]]).stream(m),s=f.translate([o-.205*t,r+.212*t]).clipExtent([[o-.214*t+x,r+.166*t+x],[o-.115*t-x,r+.234*t-x]]).stream(m),n},n.drawCompositionBorders=function(n){var e=p([-102.91,26.3]),t=p([-104,27.5]),o=p([-108,29.1]),r=p([-110,29.1]),a=p([-110,26.7]),i=p([-112.8,27.6]),l=p([-114.3,30.6]),s=p([-119.3,30.1]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.moveTo(a[0],a[1]),n.lineTo(i[0],i[1]),n.lineTo(l[0],l[1]),n.lineTo(s[0],s[1])},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(1070)}function a(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function i(){function n(n){var e=n[0],t=n[1];return f=null,i.point(e,t),f||(l.point(e,t),f)||(s.point(e,t),f)||(c.point(e,t),f)||(p.point(e,t),f)||(u.point(e,t),f)}var o,r,i,l,s,c,p,u,f,m=e.geoAlbers(),v=e.geoConicEqualArea().rotate([154,0]).center([-2,58.5]).parallels([55,65]),T=e.geoConicEqualArea().rotate([157,0]).center([-3,19.9]).parallels([8,18]),g=e.geoConicEqualArea().rotate([66,0]).center([0,18]).parallels([8,18]),h=e.geoEquirectangular().rotate([173,14]),d=e.geoEquirectangular().rotate([-145,-16.8]),E={point:function(n,e){f=[n,e]}};return n.invert=function(n){var e=m.scale(),t=m.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.12&&r<.234&&o>=-.425&&o<-.214?v:r>=.166&&r<.234&&o>=-.214&&o<-.115?T:r>=.2064&&r<.2413&&o>=.312&&o<.385?g:r>=.09&&r<.1197&&o>=-.4243&&o<-.3232?h:r>=-.0518&&r<.0895&&o>=-.4243&&o<-.3824?d:m).invert(n)},n.stream=function(n){return o&&r===n?o:o=a([m.stream(r=n),v.stream(n),T.stream(n),g.stream(n),h.stream(n),d.stream(n)])},n.precision=function(e){return arguments.length?(m.precision(e),v.precision(e),T.precision(e),g.precision(e),h.precision(e),d.precision(e),n):m.precision()},n.scale=function(e){return arguments.length?(m.scale(e),v.scale(.35*e),T.scale(e),g.scale(e),h.scale(2*e),d.scale(e),n.translate(m.translate())):m.scale()},n.translate=function(e){if(!arguments.length)return m.translate();var t=m.scale(),o=+e[0],r=+e[1];return i=m.translate(e).clipExtent([[o-.455*t,r-.238*t],[o+.455*t,r+.238*t]]).stream(E),l=v.translate([o-.307*t,r+.201*t]).clipExtent([[o-.425*t+x,r+.12*t+x],[o-.214*t-x,r+.233*t-x]]).stream(E),s=T.translate([o-.205*t,r+.212*t]).clipExtent([[o-.214*t+x,r+.166*t+x],[o-.115*t-x,r+.233*t-x]]).stream(E),c=g.translate([o+.35*t,r+.224*t]).clipExtent([[o+.312*t+x,r+.2064*t+x],[o+.385*t-x,r+.233*t-x]]).stream(E),p=h.translate([o-.492*t,r+.09*t]).clipExtent([[o-.4243*t+x,r+.0903*t+x],[o-.3233*t-x,r+.1197*t-x]]).stream(E),u=d.translate([o-.408*t,r+.018*t]).clipExtent([[o-.4244*t+x,r-.0519*t+x],[o-.3824*t-x,r+.0895*t-x]]).stream(E),n},n.drawCompositionBorders=function(n){var e=m([-110.4641,28.2805]),t=m([-104.0597,28.9528]),o=m([-103.7049,25.1031]),r=m([-109.8337,24.4531]),a=m([-124.4745,28.1407]),i=m([-110.931,30.8844]),l=m([-109.8337,24.4531]),s=m([-122.4628,21.8562]),c=m([-76.8579,25.1544]),p=m([-72.429,24.2097]),u=m([-72.8265,22.7056]),f=m([-77.1852,23.6392]),v=m([-125.0093,29.7791]),T=m([-118.5193,31.3262]),g=m([-118.064,29.6912]),h=m([-124.4369,28.169]),d=m([-128.1314,37.4582]),E=m([-125.2132,38.214]),C=m([-122.3616,30.5115]),x=m([-125.0315,29.8211]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),n.moveTo(a[0],a[1]),n.lineTo(i[0],i[1]),n.lineTo(l[0],l[1]),n.lineTo(l[0],l[1]),n.lineTo(s[0],s[1]),n.closePath(),n.moveTo(c[0],c[1]),n.lineTo(p[0],p[1]),n.lineTo(u[0],u[1]),n.lineTo(u[0],u[1]),n.lineTo(f[0],f[1]),n.closePath(),n.moveTo(v[0],v[1]),n.lineTo(T[0],T[1]),n.lineTo(g[0],g[1]),n.lineTo(g[0],g[1]),n.lineTo(h[0],h[1]),n.closePath(),n.moveTo(d[0],d[1]),n.lineTo(E[0],E[1]),n.lineTo(C[0],C[1]),n.lineTo(C[0],C[1]),n.lineTo(x[0],x[1]),n.closePath()},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(1070)}function l(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function s(){function n(n){var e=n[0],t=n[1];return s=null,a.point(e,t),s||(i.point(e,t),s)}var o,r,a,i,s,c=e.geoConicConformal().rotate([5,-38.6]).parallels([0,60]),p=e.geoConicConformal().rotate([5,-38.6]).parallels([0,60]),u={point:function(n,e){s=[n,e]}};return n.invert=function(n){var e=c.scale(),t=c.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.05346&&r<.0897&&o>=-.13388&&o<-.0322?p:c).invert(n)},n.stream=function(n){return o&&r===n?o:o=l([c.stream(r=n),p.stream(n)])},n.precision=function(e){return arguments.length?(c.precision(e),p.precision(e),n):c.precision()},n.scale=function(e){return arguments.length?(c.scale(e),p.scale(e),n.translate(c.translate())):c.scale()},n.translate=function(e){if(!arguments.length)return c.translate();var t=c.scale(),o=+e[0],r=+e[1];return a=c.translate(e).clipExtent([[o-.06857*t,r-.1288*t],[o+.13249*t,r+.05292*t]]).stream(u),i=p.translate([o+.1*t,r-.094*t]).clipExtent([[o-.1331*t+x,r+.053457*t+x],[o-.0354*t-x,r+.08969*t-x]]).stream(u),n},n.drawCompositionBorders=function(n){var e=c([-14.0346750522884,34.96500729877966]),t=c([-7.4208899681602025,35.53698899616862]),o=c([-7.314827535125545,33.54359498636456]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1])},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(2700)}function c(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function p(){function n(n){var e=n[0],t=n[1];return s=null,a.point(e,t),s||(i.point(e,t),s)||(l.point(e,t),s)}var o,r,a,i,l,s,p=e.geoConicConformal().rotate([10,-39.3]).parallels([0,60]),u=e.geoConicConformal().rotate([17,-32.7]).parallels([0,60]),f=e.geoConicConformal().rotate([27.8,-38.6]).parallels([0,60]),m={point:function(n,e){s=[n,e]}};return n.invert=function(n){var e=p.scale(),t=p.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.0093&&r<.03678&&o>=-.03875&&o<-.0116?u:r>=-.0412&&r<.0091&&o>=-.07782&&o<-.01166?f:p).invert(n)},n.stream=function(n){return o&&r===n?o:o=c([p.stream(r=n),u.stream(n),f.stream(n)])},n.precision=function(e){return arguments.length?(p.precision(e),u.precision(e),f.precision(e),n):p.precision()},n.scale=function(e){return arguments.length?(p.scale(e),u.scale(e),f.scale(.6*e),n.translate(p.translate())):p.scale()},n.translate=function(e){if(!arguments.length)return p.translate();var t=p.scale(),o=+e[0],r=+e[1];return a=p.translate(e).clipExtent([[o-.0115*t,r-.1138*t],[o+.2105*t,r+.0673*t]]).stream(m),i=u.translate([o-.0265*t,r+.025*t]).clipExtent([[o-.0388*t+x,r+.0093*t+x],[o-.0116*t-x,r+.0368*t-x]]).stream(m),l=f.translate([o-.045*t,r+-.02*t]).clipExtent([[o-.0778*t+x,r-.0413*t+x],[o-.0117*t-x,r+.0091*t-x]]).stream(m),n},n.drawCompositionBorders=function(n){var e=p([-12.8351,38.7113]),t=p([-10.8482,38.7633]),o=p([-10.8181,37.2072]),r=p([-12.7345,37.1573]),a=p([-16.0753,41.4436]),i=p([-10.9168,41.6861]),l=p([-10.8557,38.7747]),s=p([-15.6728,38.5505]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),n.moveTo(a[0],a[1]),n.lineTo(i[0],i[1]),n.lineTo(l[0],l[1]),n.lineTo(l[0],l[1]),n.lineTo(s[0],s[1]),n.closePath()},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(4200)}function u(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function f(){function n(n){var e=n[0],t=n[1];return l=null,a.point(e,t),l||(i.point(e,t),l)}var o,r,a,i,l,s=e.geoMercator().rotate([80,1.5]),c=e.geoMercator().rotate([90.73,1]),p={point:function(n,e){l=[n,e]}};return n.invert=function(n){var e=s.scale(),t=s.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=-.0676&&r<-.026&&o>=-.0857&&o<-.0263?c:s).invert(n)},n.stream=function(n){return o&&r===n?o:o=u([s.stream(r=n),c.stream(n)])},n.precision=function(e){return arguments.length?(s.precision(e),c.precision(e),n):s.precision()},n.scale=function(e){return arguments.length?(s.scale(e),c.scale(e),n.translate(s.translate())):s.scale()},n.translate=function(e){if(!arguments.length)return s.translate();var t=s.scale(),o=+e[0],r=+e[1];return a=s.translate(e).clipExtent([[o-.0262*t,r-.0734*t],[o+.1741*t,r+.079*t]]).stream(p),i=c.translate([o-.06*t,r-.04*t]).clipExtent([[o-.0857*t+x,r-.0676*t+x],[o-.0263*t-x,r-.026*t-x]]).stream(p),n},n.drawCompositionBorders=function(n){var e=s([-84.9032,2.3757]),t=s([-81.5047,2.3708]),o=s([-81.5063,-.01]),r=s([-84.9086,-.005]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath()},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(3500)}function m(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function v(){function n(n){var e=n[0],t=n[1];return c=null,a.point(e,t),c||(i.point(e,t),c)||(l.point(e,t),c)||(s.point(e,t),c)}var o,r,a,i,l,s,c,p=e.geoTransverseMercator().rotate([72,37]),u=e.geoStereographic().rotate([72,0]),f=e.geoMercator().rotate([80,33.5]),v=e.geoMercator().rotate([110,25]),T={point:function(n,e){c=[n,e]}};return n.invert=function(n){var e=p.scale(),t=p.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.2582&&r<.32&&o>=-.1036&&o<-.087?u:r>=-.01298&&r<.0133&&o>=-.11396&&o<-.05944?f:r>=.01539&&r<.03911&&o>=-.089&&o<-.0588?v:p).invert(n)},n.stream=function(n){return o&&r===n?o:o=m([p.stream(r=n),u.stream(n),f.stream(n),v.stream(n)])},n.precision=function(e){return arguments.length?(p.precision(e),u.precision(e),f.precision(e),v.precision(e),n):p.precision()},n.scale=function(e){return arguments.length?(p.scale(e),u.scale(.15*e),f.scale(1.5*e),v.scale(1.5*e),n.translate(p.translate())):p.scale()},n.translate=function(e){if(!arguments.length)return p.translate();var t=p.scale(),o=+e[0],r=+e[1];return a=p.translate(e).clipExtent([[o-.059*t,r-.3835*t],[o+.4498*t,r+.3375*t]]).stream(T),i=u.translate([o-.087*t,r+.17*t]).clipExtent([[o-.1166*t+x,r+.2582*t+x],[o-.06*t-x,r+.32*t-x]]).stream(T),l=f.translate([o-.092*t,r-0*t]).clipExtent([[o-.114*t+x,r-.013*t+x],[o-.0594*t-x,r+.0133*t-x]]).stream(T),s=v.translate([o-.089*t,r-.0265*t]).clipExtent([[o-.089*t+x,r+.0154*t+x],[o-.0588*t-x,r+.0391*t-x]]).stream(T),n},n.drawCompositionBorders=function(n){var e=p([-82.6999,-51.3043]),t=p([-77.5442,-51.6631]),o=p([-78.0254,-55.186]),r=p([-83.6106,-54.7785]),a=p([-80.0638,-35.984]),i=p([-76.2153,-36.1811]),l=p([-76.2994,-37.6839]),s=p([-80.2231,-37.4757]),c=p([-78.442,-37.706]),u=p([-76.263,-37.8054]),f=p([-76.344,-39.1595]),m=p([-78.5638,-39.0559]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),n.moveTo(a[0],a[1]),n.lineTo(i[0],i[1]),n.lineTo(l[0],l[1]),n.lineTo(l[0],l[1]),n.lineTo(s[0],s[1]),n.closePath(),n.moveTo(c[0],c[1]),n.lineTo(u[0],u[1]),n.lineTo(f[0],f[1]),n.lineTo(f[0],f[1]),n.lineTo(m[0],m[1]),n.closePath()},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(700)}function T(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function g(){function n(n){var e=n[0],t=n[1];return s=null,a.point(e,t),s||(i.point(e,t),s)||(l.point(e,t),s)}var o,r,a,i,l,s,c=e.geoConicEquidistant().rotate([-136,-22]).parallels([40,34]),p=e.geoConicEquidistant().rotate([-146,-26]).parallels([40,34]),u=e.geoConicEquidistant().rotate([-126,-19]).parallels([40,34]),f={point:function(n,e){s=[n,e]}};return n.invert=function(n){var e=c.scale(),t=c.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=-.10925&&r<-.02701&&o>=-.135&&o<-.0397?p:r>=.04713&&r<.11138&&o>=-.03986&&o<.051?u:c).invert(n)},n.stream=function(n){return o&&r===n?o:o=T([c.stream(r=n),p.stream(n),u.stream(n)])},n.precision=function(e){return arguments.length?(c.precision(e),p.precision(e),u.precision(e),n):c.precision()},n.scale=function(e){return arguments.length?(c.scale(e),p.scale(e),u.scale(.7*e),n.translate(c.translate())):c.scale()},n.translate=function(e){if(!arguments.length)return c.translate();var t=c.scale(),o=+e[0],r=+e[1];return a=c.translate(e).clipExtent([[o-.1352*t,r-.1091*t],[o+.117*t,r+.098*t]]).stream(f),i=p.translate([o-.0425*t,r-.005*t]).clipExtent([[o-.135*t+x,r-.1093*t+x],[o-.0397*t-x,r-.027*t-x]]).stream(f),l=u.translate(e).clipExtent([[o-.0399*t+x,r+.0471*t+x],[o+.051*t-x,r+.1114*t-x]]).stream(f),n},n.drawCompositionBorders=function(n){var e=c([126.01320483689143,41.621090310215585]),t=c([133.04304387025903,42.15087523707186]),o=c([133.3021766080688,37.43975444725098]),r=c([126.87889168628224,36.95488945159779]),a=c([132.9,29.8]),i=c([134,33]),l=c([139.3,33.2]),s=c([139.16,30.5]);n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),n.moveTo(a[0],a[1]),n.lineTo(i[0],i[1]),n.lineTo(l[0],l[1]),n.lineTo(s[0],s[1])},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(2200)}function h(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function d(){function n(n){var e=n[0],t=n[1];return d=null,a.point(e,t),d||(i.point(e,t),d)||(l.point(e,t),d)||(s.point(e,t),d)||(c.point(e,t),d)||(p.point(e,t),d)||(u.point(e,t),d)||(f.point(e,t),d)||(m.point(e,t),d)||(v.point(e,t),d)||(T.point(e,t),d)||(g.point(e,t),d)}var o,r,a,i,l,s,c,p,u,f,m,v,T,g,d,E=e.geoConicConformal().rotate([-3,-46.2]).parallels([0,60]),C=e.geoMercator().center([-53.2,3.9]),S=e.geoMercator().center([-61.03,14.67]),y=e.geoMercator().center([-61.46,16.14]),P=e.geoMercator().center([-62.85,17.92]),B=e.geoMercator().center([-56.23,46.93]),M=e.geoMercator().center([45.16,-12.8]),w=e.geoMercator().center([55.52,-21.13]),q=e.geoMercator().center([165.8,-21.07]),A=e.geoMercator().center([-178.1,-14.3]),b=e.geoMercator().center([-150.55,-17.11]),j=e.geoMercator().center([-150.55,-17.11]),U={point:function(n,e){d=[n,e]}};return n.invert=function(n){var e=E.scale(),t=E.translate(),o=(n[0]-t[0])/e,r=(n[1]-t[1])/e;return(r>=.029&&r<.0864&&o>=-.14&&o<-.0996?C:r>=0&&r<.029&&o>=-.14&&o<-.0996?S:r>=-.032&&r<0&&o>=-.14&&o<-.0996?y:r>=-.052&&r<-.032&&o>=-.14&&o<-.0996?P:r>=-.076&&r<.052&&o>=-.14&&o<-.0996?B:r>=-.076&&r<-.052&&o>=.0967&&o<.1371?M:r>=-.052&&r<-.02&&o>=.0967&&o<.1371?w:r>=-.02&&r<.012&&o>=.0967&&o<.1371?q:r>=.012&&r<.033&&o>=.0967&&o<.1371?A:r>=.033&&r<.0864&&o>=.0967&&o<.1371?b:E).invert(n)},n.stream=function(n){return o&&r===n?o:o=h([E.stream(r=n),C.stream(n),S.stream(n),y.stream(n),P.stream(n),B.stream(n),M.stream(n),w.stream(n),q.stream(n),A.stream(n),b.stream(n),j.stream(n)])},n.precision=function(e){return arguments.length?(E.precision(e),C.precision(e),S.precision(e),y.precision(e),P.precision(e),B.precision(e),M.precision(e),w.precision(e),q.precision(e),A.precision(e),b.precision(e),j.precision(e),n):E.precision()},n.scale=function(e){return arguments.length?(E.scale(e),C.scale(.6*e),S.scale(1.6*e),y.scale(1.4*e),P.scale(5*e),B.scale(1.3*e),M.scale(1.6*e),w.scale(1.2*e),q.scale(.3*e),A.scale(2.7*e),b.scale(.5*e),j.scale(.06*e),n.translate(E.translate())):E.scale()},n.translate=function(e){if(!arguments.length)return E.translate();var t=E.scale(),o=+e[0],r=+e[1];return a=E.translate(e).clipExtent([[o-.0996*t,r-.0908*t],[o+.0967*t,r+.0864*t]]).stream(U),i=C.translate([o-.12*t,r+.0575*t]).clipExtent([[o-.14*t+x,r+.029*t+x],[o-.0996*t-x,r+.0864*t-x]]).stream(U),l=S.translate([o-.12*t,r+.013*t]).clipExtent([[o-.14*t+x,r+0*t+x],[o-.0996*t-x,r+.029*t-x]]).stream(U),s=y.translate([o-.12*t,r-.014*t]).clipExtent([[o-.14*t+x,r-.032*t+x],[o-.0996*t-x,r+0*t-x]]).stream(U),c=P.translate([o-.12*t,r-.044*t]).clipExtent([[o-.14*t+x,r-.052*t+x],[o-.0996*t-x,r-.032*t-x]]).stream(U),p=B.translate([o-.12*t,r-.065*t]).clipExtent([[o-.14*t+x,r-.076*t+x],[o-.0996*t-x,r-.052*t-x]]).stream(U),u=M.translate([o+.117*t,r-.064*t]).clipExtent([[o+.0967*t+x,r-.076*t+x],[o+.1371*t-x,r-.052*t-x]]).stream(U),f=w.translate([o+.116*t,r-.0355*t]).clipExtent([[o+.0967*t+x,r-.052*t+x],[o+.1371*t-x,r-.02*t-x]]).stream(U),m=q.translate([o+.116*t,r-.0048*t]).clipExtent([[o+.0967*t+x,r-.02*t+x],[o+.1371*t-x,r+.012*t-x]]).stream(U),v=A.translate([o+.116*t,r+.022*t]).clipExtent([[o+.0967*t+x,r+.012*t+x],[o+.1371*t-x,r+.033*t-x]]).stream(U),g=j.translate([o+.11*t,r+.045*t]).clipExtent([[o+.0967*t+x,r+.033*t+x],[o+.1371*t-x,r+.06*t-x]]).stream(U),T=b.translate([o+.115*t,r+.075*t]).clipExtent([[o+.0967*t+x,r+.06*t+x],[o+.1371*t-x,r+.0864*t-x]]).stream(U),n},n.drawCompositionBorders=function(n){var e,t,o,r;e=E([-7.938886725111036,43.7219460918835]),t=E([-4.832080896458295,44.12930268549372]),o=E([-4.205299743793263,40.98096346967365]),r=E([-7.071796453126152,40.610037319181444]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([-8.42751373617692,45.32889452553031]),t=E([-5.18599305777107,45.7566442062976]),o=E([-4.832080905154431,44.129302726751426]),r=E([-7.938886737126192,43.72194613263854]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([-9.012656899657046,47.127733821030176]),t=E([-5.6105244772793155,47.579777861410626]),o=E([-5.185993067168585,45.756644248170346]),r=E([-8.427513749141811,45.32889456686326]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([-9.405747558985553,48.26506375557457]),t=E([-5.896175018439575,48.733352850851624]),o=E([-5.610524487556043,47.57977790393761]),r=E([-9.012656913808351,47.127733862971255]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([-9.908436061346974,49.642448789505856]),t=E([-6.262026716233124,50.131426841787174]),o=E([-5.896175029331232,48.73335289377258]),r=E([-9.40574757396393,48.26506379787767]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([11.996907706504462,50.16039028163579]),t=E([15.649907879773343,49.68279246765253]),o=E([15.156712840526632,48.30371557625831]),r=E([11.64122661754411,48.761078240546816]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([11.641226606955788,48.7610781975889]),t=E([15.156712825832164,48.30371553390465]),o=E([14.549932166241172,46.4866532486199]),r=E([11.204443787952183,46.91899233914248]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([11.204443778297161,46.918992296823646]),t=E([14.549932152815039,46.486653206856396]),o=E([13.994409796764009,44.695833444323256]),r=E([10.805306599253848,45.105133870684924]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([10.805306590412085,45.10513382903308]),t=E([13.99440978444733,44.695833403183606]),o=E([13.654633799024392,43.53552468558152]),r=E([10.561516803980956,43.930671459798624]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([10.561516795617383,43.93067141859757]),t=E([13.654633787361952,43.5355246448671]),o=E([12.867691604239901,40.640701985019405]),r=E([9.997809515987688,41.00288343254471]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=E([10.8,42.4]),t=E([12.8,42.13]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1])},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(2700)}function E(n){var e=n.length;return{point:function(t,o){for(var r=-1;++r<e;)n[r].point(t,o)},sphere:function(){for(var t=-1;++t<e;)n[t].sphere()},lineStart:function(){for(var t=-1;++t<e;)n[t].lineStart()},lineEnd:function(){for(var t=-1;++t<e;)n[t].lineEnd()},polygonStart:function(){for(var t=-1;++t<e;)n[t].polygonStart()},polygonEnd:function(){for(var t=-1;++t<e;)n[t].polygonEnd()}}}function C(){function n(n){var e=n[0],t=n[1];return h=null,a.point(e,t),h||(l.point(e,t),h)||(m.point(e,t),h)||(i.point(e,t),h)||(f.point(e,t),h)||(u.point(e,t),h)||(v.point(e,t),h)||(T.point(e,t),h)||(g.point(e,t),h)||(s.point(e,t),h)||(c.point(e,t),h)||(p.point(e,t),h)}var o,r,a,i,l,s,c,p,u,f,m,v,T,g,h,d=e.geoConicConformal().rotate([-10,-53]).parallels([0,60]),C=e.geoMercator().center([-61.46,16.14]),S=e.geoMercator().center([-53.2,3.9]),y=e.geoConicConformal().rotate([27.8,-38.9]).parallels([0,60]),P=e.geoConicConformal().rotate([25.43,-37.398]).parallels([0,60]),B=e.geoConicConformal().rotate([31.17,-39.539]).parallels([0,60]),M=e.geoConicConformal().rotate([17,-32.7]).parallels([0,60]),w=e.geoConicConformal().rotate([16,-28.5]).parallels([0,60]),q=e.geoMercator().center([-61.03,14.67]),A=e.geoMercator().center([45.16,-12.8]),b=e.geoMercator().center([55.52,-21.13]),j=e.geoConicConformal().rotate([-14.4,-35.95]).parallels([0,60]),U={point:function(n,e){h=[n,e]}};return n.invert=function(n){var e=d.scale(),t=d.translate(),o=(n[0]-(t[0]+.08*e))/e,r=(n[1]-t[1])/e;return(r>=-.31&&r<-.24&&o>=.14&&o<.24?C:r>=-.24&&r<-.17&&o>=.14&&o<.24?S:r>=-.17&&r<-.12&&o>=.21&&o<.24?P:r>=-.17&&r<-.14&&o>=.14&&o<.165?B:r>=-.17&&r<-.1&&o>=.14&&o<.24?y:r>=-.1&&r<-.03&&o>=.14&&o<.24?M:r>=-.03&&r<.04&&o>=.14&&o<.24?w:r>=-.31&&r<-.24&&o>=.24&&o<.34?q:r>=-.24&&r<-.17&&o>=.24&&o<.34?A:r>=-.17&&r<-.1&&o>=.24&&o<.34?b:r>=-.1&&r<-.03&&o>=.24&&o<.34?j:d).invert(n)},n.stream=function(n){return o&&r===n?o:o=E([d.stream(r=n),S.stream(n),q.stream(n),C.stream(n),w.stream(n),M.stream(n),A.stream(n),b.stream(n),j.stream(n),y.stream(n),P.stream(n),B.stream(n)])},n.precision=function(e){return arguments.length?(d.precision(e),S.precision(e),q.precision(e),C.precision(e),w.precision(e),M.precision(e),A.precision(e),b.precision(e),j.precision(e),y.precision(e),P.precision(e),B.precision(e),n):d.precision()},n.scale=function(e){return arguments.length?(d.scale(e),C.scale(3*e),S.scale(.8*e),q.scale(3.5*e),b.scale(2.7*e),y.scale(2*e),P.scale(2*e),B.scale(2*e),M.scale(3*e),w.scale(e),A.scale(5.5*e),j.scale(6*e),n.translate(d.translate())):d.scale()},n.translate=function(e){if(!arguments.length)return d.translate();var t=d.scale(),o=+e[0],r=+e[1];return a=d.translate([o-.08*t,r]).clipExtent([[o-.51*t,r-.33*t],[o+.5*t,r+.33*t]]).stream(U),i=C.translate([o+.19*t,r-.275*t]).clipExtent([[o+.14*t+x,r-.31*t+x],[o+.24*t-x,r-.24*t-x]]).stream(U),l=S.translate([o+.19*t,r-.205*t]).clipExtent([[o+.14*t+x,r-.24*t+x],[o+.24*t-x,r-.17*t-x]]).stream(U),s=y.translate([o+.19*t,r-.135*t]).clipExtent([[o+.14*t+x,r-.17*t+x],[o+.24*t-x,r-.1*t-x]]).stream(U),c=P.translate([o+.225*t,r-.147*t]).clipExtent([[o+.21*t+x,r-.17*t+x],[o+.24*t-x,r-.12*t-x]]).stream(U),p=B.translate([o+.153*t,r-.15*t]).clipExtent([[o+.14*t+x,r-.17*t+x],[o+.165*t-x,r-.14*t-x]]).stream(U),u=M.translate([o+.19*t,r-.065*t]).clipExtent([[o+.14*t+x,r-.1*t+x],[o+.24*t-x,r-.03*t-x]]).stream(U),f=w.translate([o+.19*t,r+.005*t]).clipExtent([[o+.14*t+x,r-.03*t+x],[o+.24*t-x,r+.04*t-x]]).stream(U),m=q.translate([o+.29*t,r-.275*t]).clipExtent([[o+.24*t+x,r-.31*t+x],[o+.34*t-x,r-.24*t-x]]).stream(U),v=A.translate([o+.29*t,r-.205*t]).clipExtent([[o+.24*t+x,r-.24*t+x],[o+.34*t-x,r-.17*t-x]]).stream(U),T=b.translate([o+.29*t,r-.135*t]).clipExtent([[o+.24*t+x,r-.17*t+x],[o+.34*t-x,r-.1*t-x]]).stream(U),g=j.translate([o+.29*t,r-.065*t]).clipExtent([[o+.24*t+x,r-.1*t+x],[o+.34*t-x,r-.03*t-x]]).stream(U),n},n.drawCompositionBorders=function(n){var e,t,o,r;e=d([42.45755610828648,63.343658547914934]),t=d([52.65837266667029,59.35045080290929]),o=d([47.19754502247785,56.12653496548117]),r=d([37.673034273363044,59.61638268506111]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([59.41110754003403,62.35069727399336]),t=d([66.75050228640794,57.11797303636038]),o=d([60.236065725110436,54.63331433818992]),r=d([52.65837313153311,59.350450804599355]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([48.81091130080243,66.93353402634641]),t=d([59.41110730654679,62.35069740653086]),o=d([52.6583728974441,59.3504509222445]),r=d([42.45755631675751,63.34365868805821]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([31.054198418446475,52.1080673766184]),t=d([39.09869284884117,49.400700047190554]),o=d([36.0580811499175,46.02944174908498]),r=d([28.690508588835726,48.433126979386415]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([33.977877745912025,55.849945501331]),t=d([42.75328432167726,52.78455122462353]),o=d([39.09869297540224,49.400700176148625]),r=d([31.05419851807008,52.10806751810923]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([52.658372900759296,59.35045068526415]),t=d([60.23606549583304,54.63331423800264]),o=d([54.6756370953122,51.892298789399455]),r=d([47.19754524788189,56.126534861222794]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([47.19754506082455,56.126534735591456]),t=d([54.675636900123514,51.892298681337095]),o=d([49.94448648951486,48.98775484983285]),r=d([42.75328468716108,52.78455126060818]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([42.75328453416769,52.78455113209101]),t=d([49.94448632339758,48.98775473706457]),o=d([45.912339990394315,45.99361784987003]),r=d([39.09869317356607,49.40070009378711]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([37.673034114296634,59.61638254183119]),t=d([47.197544835420544,56.126534839849846]),o=d([42.75328447467064,52.78455135314068]),r=d([33.977877870363905,55.849945644671145]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([44.56748486446032,57.26489367845818]),o=d([43.9335791193588,53.746540942601726]),r=d([43,56]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath(),e=d([37.673034114296634,59.61638254183119]),t=d([40.25902691953466,58.83002044222639]),o=d([38.458270492742024,57.26232178028002]),r=d([35.97754948030156,58.00266637992386]),n.moveTo(e[0],e[1]),n.lineTo(t[0],t[1]),n.lineTo(o[0],o[1]),n.lineTo(r[0],r[1]),n.closePath()},n.getCompositionBorders=function(){var n=t.path();return this.drawCompositionBorders(n),n.toString()},n.scale(750)}var x=1e-6;n.geoAlbersUsa=r,n.geoAlbersUsaTerritories=i,n.geoConicConformalSpain=s,n.geoConicConformalPortugal=p,n.geoMercatorEcuador=f,n.geoTransverseMercatorChile=v,n.geoConicEquidistantJapan=g,n.geoConicConformalFrance=d,n.geoConicConformalEurope=C,Object.defineProperty(n,"__esModule",{value:!0})});
from osgeo import ogr
def get_length(geom):
length = 0.0
if geom.GetGeometryName() == 'POLYGON':
length += get_polygon_length(geom)
else:
for polygon in geom:
length += get_polygon_length(polygon)
return length
def get_polygon_length(polygon):
length = 0.0
for ring in polygon:
length += ring.Length()
return length
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open("cgd114p010g.shp", 1)
layer = dataSource.GetLayer()
new_field = ogr.FieldDefn("Area", ogr.OFTReal)
new_field.SetWidth(32)
new_field.SetPrecision(2) #added line to set precision
layer.CreateField(new_field)
for feature in layer:
geom = feature.GetGeometryRef()
area = geom.GetArea()
print feature.GetFieldAsString(0), area, get_length(geom)
feature.SetField("Area", area)
layer.SetFeature(feature)
dataSource = None
<!DOCTYPE html>
<meta charset="utf-8">
<style>
#tooltip {
position: absolute;
top: 0;
left: 0;
z-index: 10;
margin: 0;
padding: 10px;
width: 200px;
height: 70px;
color: white;
font-family: sans-serif;
font-size: 0.9em;
font-weight: bold;
text-align: center;
background-color: rgba(0, 0, 0, 0.55);
opacity: 0;
pointer-events: none;
border-radius:5px;
transition: .2s;
}
</style>
<body>
<div id="container"/>
<div id="tooltip"/>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-composite-projections/1.0.1/d3-composite-projections.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geoAlbersUsaTerritories();
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("#container").append("svg")
.attr("width", width)
.attr("height", height);
var t = d3.transition();
d3.json("us_congressional_districts.json", function(error, us) {
var us = topojson.feature(us, us.objects.us_congressional_districts);
svg.selectAll(".region")
.data(us.features)
.enter()
.append("path")
.attr("class", "region")
.attr("d", path)
.style("fill", function(d){
if(d.properties.PARTY_AFF=="Democrat")
return "#4a86e8";
else
return "#e84a4a";})
.style("stroke", "#000")
.style("stroke-width", "0.5px")
.on("mouseover", function(d){
//Show the tooltip
var x = d3.event.pageX;
var y = d3.event.pageY - 40;
d3.select("#tooltip")
.style("left", x + "px")
.style("top", y + "px")
.style("opacity", 1)
.html( d.properties.STATE + " dist: " + d.properties.CONG_DIST + "<br/>" +d.properties.CONG_REP + "<br/>" + d.properties.PARTY_AFF );
})
.on("mouseout", function(){
//Hide the tooltip
d3.select("#tooltip")
.style("opacity", 0);
});;
svg
.append("path")
.style("fill","none")
.style("stroke","#000")
.style("stroke-dasharray","5,5")
.attr("d", projection.getCompositionBorders());
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

@bradoyler
Copy link

@rveciana So u pulled the GU, AS & MS polygons from http://www.naturalearthdata.com/downloads/10m-cultural-vectors/ (Admin 1 – States, Provinces) and then added the congress data (via wikipedia) to those geometries?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment