0

I have an image in python. It's a map of california, and I need to place some point on this map.

The coordonate of each point are retrieve from a csv. But the value of each coordinate are in latitude/longitude. So, i need to convert it to the dimension of my picture.

So, here's is the description of my situation:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# dpi for the saved figure: https://stackoverflow.com/a/34769840/3129414
dpi = 120

img = mpimg.imread("california_map_blank.png")
height, width, bands = img.shape

# Update figure size based on image size
figsize = width / float(dpi), height / float(dpi)

# Create a figure of the right size with one axes that takes up the full figure
figure = plt.figure(figsize=figsize)
axes = figure.add_axes([0, 0, 1, 1])

# Draw the image
axes.imshow(img, interpolation='nearest')

Here's the result:

enter image description here

First i need to modify the y-axis. I need to inverse it so the 0 start at the bottom. Then I need to modify the value of the axis, [31,42] for y-axis and [-123,-114] for x-axis. Because the point I want to place in this map are all in this range. One example of coordinate: 41.76440000093729, -124.1998.

Now here's my question. Is it possible to achieve this ? How ?

PS: I use python 3.6, and I already know how to place point on the image. I don't need to save the image just showing.

PPS: My final goal in fact is to convert lat/lon data into coordinate in a picture so if you know any other way to do it(in Python of course) please tell me.

EDIT: If I apply this: axes.set_xlim(-124.5,-114) it give me this:

enter image description here

I want to have the axis with this range but with the whole image.

In fact, at the end I will not display the axis I will just put the map with the points, but I need to place the point on the map so I think I need to go through this step.

EDIT2: I tried this: axes.imshow(img[::-1], origin='lower', interpolation='nearest') it works fine to reverse the axis but when I draw a point python draw it in the same place when I the axis was normal.

bosskay972
  • 890
  • 1
  • 6
  • 27
  • Can you give an equation that relates the lat/long coordinate to the desired image coordinate? If not, then you have a math question, not a programming question. – Karl Knechtel Jan 10 '20 at 21:07
  • In fact I try to construct some mathematical function with a equation system with 2 variable from the values of 2 distinct point, but I find a result that give me good hope for some point but give me big errors for other point, so I quit the mathematical way. But, right now I think i will just make a equation system with the extremum values of y axuis and x axis. Then I will be back here. – bosskay972 Jan 10 '20 at 21:12
  • But even if I do it I need at least to reverse the y axis to put point on it correctly. – bosskay972 Jan 10 '20 at 21:13
  • An equation system with 2 variables give me no solution with the extremum so if you have another proposition tell me, I continue to search in the mathematical fields – bosskay972 Jan 10 '20 at 21:25

1 Answers1

1

You need to set the limits of the image via the extent= parameter of imshow. These should be quite precise values for the longitudes left and right, and for the latitudes of bottom and top.

Depending on how deformed the map is, the result can be good enough or not. Try to find the exact longitudes and latitudes of the corners of your map, e.g. via Google Maps.

Depending on how you're running your Python program, matplotlib will show an interactive plot. You can zoom to every region, and the axes will adapt. In the bar at the bottom the x and y-positions will be shown. If they are not the desired ones, you can try to change the extents until they match.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("california_map_blank.png")  

dpi = 120
height, width, bands = img.shape
# Update figure size based on image size
figsize = width / float(dpi), height / float(dpi)

# Create a figure of the right size with one axes that takes up the full figure
fig, ax = plt.subplots(figsize=figsize)

# find the extent
longitude_top_left = -124.5
longitude_top_right = -113
latitude_bottom_left = 32
latitude_top_left = 42
extent = [longitude_top_left, longitude_top_right, latitude_bottom_left, latitude_top_left]

# Draw the image
ax.imshow(img, interpolation='nearest', extent=extent)

plt.show()

resulting image

JohanC
  • 71,591
  • 8
  • 33
  • 66