ramnathv / rMaps

Interactive Maps from R
http://rmaps.github.io
389 stars 194 forks source link

Custom topojson not displaying #21

Closed Koalha closed 10 years ago

Koalha commented 10 years ago

Hi.

And thanks for yet another inspiring package! I'm itching to get to use it, but have run to a small problem with custom topojson -files.

I'm trying to display a map of administrative borders in Finland. Some examples from here and here got me started, but I've now run to a dead end. For some reason I can't get the topojson object to display. The github repo with original shapefiles, topojson files and R-script is here (Sorry for the mess, the file I'm trying to display is kunnat2.topojson).

The file displays properly on GitHub, as you can see here

The code I've tried is as follows:

library(rMaps)

maps = Datamaps$new()

maps$set(
geographyConfig = list(
dataUrl="https://raw.github.com/Koalha/tryermap/master/kunnat2.topojson"
),

   scope = "id",
setProjection = '#! function( element, options ) {
   var projection, path;
   projection = d3.geo.transverseMercator()
    .rotate([-27,-65,0])
    .scale(2000)
    .translate([element.offsetWidth / 2, element.offsetHeight / 2]);

   path = d3.geo.path().projection( projection );
   return {path: path, projection: projection};
  } !#'
)

maps

If I comment out the lines with dataUrl and scope, I get a perfectly displaying default world map with the options that I want. The full script returns a blank page however. This makes me suspect there's something wrong with my topojson file. The file was created using topojson from the command line (Windows 8):

topojson -o kunnat.json -s 1e-7 -q 1e5 Kunnatwgs.shp -p name=text,id=nationalCo --id-property name

At first I thought it might have something to do with the umlauts in the feature names, so I tried making a topojson file with only the numerical id:s, with the same result.

Any suggestions on where to go from here? My ultimate goal is to make an animated cloropleth of population changes in the municipalities of Finland.

ekstroem commented 10 years ago

Hi,

I've been messing around the same problem with the Danish counties and have something up and running (see http://biostatistics.dk/pics/dkmap.html). Although I'm second guessing some of the things I think the name of your scope must match the name of your shape file, i.e.,

scope ="Kunnatwgs"

It's possible to merge several shape files into the same topojson file and as far as I can see you can reference them through the scope variable.

Claus

Koalha commented 10 years ago

Oh! Thank you for the help,seems I missed the point of some parameters. However, the result with scope="Kunnatwgs" still returns a blank page.

Edit: seems that commenting out doesn't help anymore with another computer and Firefox instead of chrome. I am truly lost.

Edit2: I'll see if this helps when I get home. Thanks a bunch!"

ekstroem commented 10 years ago

Here's my code - not sure if that helps:

I ran the following commands to get the topojson files running:

ogr2ogr -progress -t_srs WGS84 -simplify 300 -select KOMKODE,KOMNAVN states.shp simple.shp
topojson -o dk_kom.json  states.shp -p state_code=+KOMKODE,name=KOMNAVN --id-property KOMNAVN

The t_srs option to ogr2ogr converted the shapefile to longitude and latitute - before that it was in some other system.

From within R I use

library(rMaps)
options(RCHART_WIDTH = 600, RCHART_HEIGHT = 600)

# Read data
indata <- read.table("middelleveltid.txt", col.names=c("kommune", "life"), as.is=TRUE, na.string="..")
indata[is.na(indata)] <- 0

d1 <- ichoropleth(life ~ kommune, data = indata, ncut=8, map='states')
d1$set(
 geographyConfig = list(
  dataUrl = "/home/claus/sandbox/dk_kom.json"
 ),
scope = 'states',
setProjection = '#! function( element, options ) {
  var projection, path;
  projection = d3.geo.mercator()
   .center([10, 56]).scale(element.offsetWidth*9)
   .translate([element.offsetWidth / 2, element.offsetHeight / 2]);

  path = d3.geo.path().projection( projection );
  return {path: path, projection: projection};
 } !#'
)

d1$print('chart4', include_assets = TRUE)

I still haven't gotten the legend and labels to work.

Does your rotation value match with Finland for that projection?

Koalha commented 10 years ago

Changing the scope helped after all. A rookie mistake from me, thank you very much for your help!

ramnathv commented 10 years ago

Great that you have it resolved. Thanks @ekstroem for helping with this. I will have clearer instructions on this as I write the docs.