6

I have modified this question to reflect some progress on discovering the problem. I am using the following python code to generate an SVG of the continental USA. The shapefile is irrelevant, as the problem I describe occurs regardless of what shapefile I use. The script is really simple. I basically just take a shapefile for the US and use a bounding box to exclude Hawaii and Alaska.

from kartograph import Kartograph

K = Kartograph()

blah={
    "layers": [
        {
        "id":"mylayer",
        "src":"/Users/austinc/Documents/shapefiles/states_21basic/states.shp",
        }
    ],
    "bounds":{
        "mode":"bbox",
        "data":[-130,25,-65,50],
    },
    "proj":{
        "id":"mercator"
    }
}

K.generate(blah,outfile='/Users/austinc/Desktop/mymap.svg')

The problem is that the svg generated by this code has incorrect coordinates associated with it. When I use javascript to try to map points on it, the points appear at the correct latitude, but are off by roughly 100 degrees of longitude.

When I use a pre-made SVG from Kartograph, I do not have this problem (I have not tried cropping away Alaska and Hawaii yet). So something about my python script is causing this but I don't understand what.

FIXED: The comment below got me thinking that maybe this was something to do with the projection. I removed the part of the python script that draws the graph as a mercator projection and this fixed everything. I'm not sure I understand why but if you are having a similar issue and find this question: try changing your projection or not using a projection at all.

AustinC
  • 826
  • 1
  • 8
  • 23
  • Weird, I just got this to work, kinda. I did it by changing NY's latitude to -173. Now it's on the map. Is there some kind of geographic thing I'm fundamentally misunderstanding here? According to several online sources, NY's longitude is -73. Kartograph bug or me missing something? – AustinC Nov 17 '13 at 16:49
  • Same thing with LA. Everything is off by 100 degrees longitude. – AustinC Nov 17 '13 at 17:04
  • Have you tried to put longitudes in positive numbers? -73.94 -> 286.06 (add 360) – Roger Veciana Nov 17 '13 at 20:18
  • Yes, this was what I was trying earlier when I said I changed the longitudes. 286 doesn't work. What never occurred to me was to try other negative longitudes. It seems that if I shift all longitudes by -100 degrees everything works, which fixes the problem, but it's a very unsatisfying solution. – AustinC Nov 17 '13 at 21:01
  • And thanks for the very good tutorial Roger! – AustinC Nov 17 '13 at 21:09
  • 1
    Maybe the SVG 'mymap.svg' that you generated with the python script has the error, not the JavaScript code. If the SVG has the coordinates moved, the script will fail. Just to think about the possible problems. – Roger Veciana Nov 18 '13 at 08:21
  • This appears to be correct, see my comments on the answer below. I have posted my python code. It does not seem to matter which shape file I use, this code always results in an error. Kartograph's USA.svg does not result in the error. So now the question is, what's wrong with my Python code? – AustinC Nov 18 '13 at 13:54
  • Maybe it got something to do with the "lon0" parameter, maybe this rly sets the longitude data off by the number supplied to the parameter, which would be rly odd. – scrimau Aug 03 '15 at 11:17

1 Answers1

1

It might be that the coordinate system used in your SVG file doesn't coincide with latitudes and longitudes. You might need to know what projection was used for generating the SVG file, and computing latitudes and longitudes by using the inverse of that projection. Maybe also try to see if the same problem occurs with the ready-to-use maps provided by Kartograph.

user2314737
  • 27,088
  • 20
  • 102
  • 114
  • I think the latitudes in it are correct. I do know the projection - it is mercator. I have added the python code I used to generate the SVG to the original post. I've tried it with shape files from completely different sources as well. Could you explain what you mean by calculating lat and long using the inverse of the projection? I will try Kartograph's ready to use USA map and see what happens with it. – AustinC Nov 18 '13 at 13:02
  • Ok, I tried the Kartograph SVG and it worked perfectly. So I guess my SVGs have incorrect coordinate systems. Why would my python code generate this? It seems to me like it must have something to do with the bounding box? – AustinC Nov 18 '13 at 13:20
  • This seems to be an ongoing problem with this library. The maps I make with kartograph.py looks great but they have the wrong coordinates, so they are useless for plotting symbols or anything else with kartograph.js. When I used the Kartograph SVGs linked above it worked for me as well. I would say this is a huge issue and from the other question, http://stackoverflow.com/questions/27799912/kartograph-add-symbol-coordinates it looks like it has to do with the map projections. This should really be addressed by the library's creator. – cheenbabes Jun 18 '15 at 00:25