Plotting a grid in km with a center point in latitude and longitude onto a map

53 Views Asked by At

I want to plot a square grid of size 2km by 2km with the point (latitude, longitude) in degrees:

co_ord = (47.9187393, 106.9175013)

located at the center of the square grid.

I have tried:

import pandas as pd
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# setting the size of the map
fig = plt.figure(figsize=(12,9))

# create the map - setting latitude and longitude
m = Basemap( projection = 'mill', llcrnrlat = 47.85, urcrnrlat = 48.1, llcrnrlon = 106.2, urcrnrlon = 107.1, resolution ='h')

m.drawcoastlines()
m.drawcountries(color='gray')
m.drawstates(color='gray')

# creating variable for latitude, longitude to list
lat = 47.9187393 
lon = 106.9175013

# plotting the map
m.scatter(lon, lat, latlon = True, s = 10, c = 'red', marker = 'o', alpha = 1)

plt.show()

However, I would also like to display geographical locations along with my x axis and y axis of the plot being in metres with the co-ordinate being at (0,0) of the plot.

EDIT

The code from jinyao gives the following image. The scale is off and there is still no geographical features (like rivers, lakes, fields).

enter image description here

1

There are 1 best solutions below

2
jinyao On

Use xticks and yticks to draw ticks then add label.

Code:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

# setting the size of the map
fig = plt.figure(figsize=(12,9))

# create the map - setting latitude and longitude
m = Basemap(projection='mill', llcrnrlat=47.85, urcrnrlat=48.1, llcrnrlon=106.2, urcrnrlon=107.1, resolution='h')

m.drawcoastlines()
m.drawcountries(color='gray')
m.drawstates(color='gray')

# Define the center point
center_lat, center_lon = 47.9187393, 106.9175013

# Convert center point to meters
center_x, center_y = m(center_lon, center_lat)

# Define the size of the square grid in meters
grid_size = 2000  # 2km

# Define the boundaries of the square grid
x_min, x_max = center_x - grid_size / 2, center_x + grid_size / 2
y_min, y_max = center_y - grid_size / 2, center_y + grid_size / 2

# Plot the square grid
m.plot([x_min, x_min, x_max, x_max, x_min], [y_min, y_max, y_max, y_min, y_min], color='blue')

# Plot the center point
m.scatter(center_lon, center_lat, latlon=True, s=50, c='red', marker='o', alpha=1)

# Convert meters to geographical coordinates for ticks
def meters_to_lonlat(x, y):
    lon, lat = m(x, y, inverse=True)
    return lon, lat

# Define x and y ticks
x_ticks = np.arange(x_min, x_max, 500)
y_ticks = np.arange(y_min, y_max, 500)

# Convert ticks to lon/lat
lon_ticks, lat_ticks = meters_to_lonlat(x_ticks, y_ticks)

# Plot grid ticks
m.scatter(lon_ticks, lat_ticks, latlon=True, s=10, c='black', marker='.')

# Set x and y axis labels to meters
plt.xlabel('X (meters)')
plt.ylabel('Y (meters)')

# Set the tick labels
plt.xticks(x_ticks, [f'{int(t)}\n({int(t - center_x)}m)' for t in x_ticks])
plt.yticks(y_ticks, [f'{int(t)}\n({int(t - center_y)}m)' for t in y_ticks])

plt.show()

pyplot xticks