Python - Divide into Grids given Latitude and Longitude

Viewed 16

I am working on geographical data and want to divide an area of interest into grids of 4*4 each. How can I do the same in python ? I have the lat and long of the upper right corner and lower left corner of the bounding box ?

min_lon, min_lat = (76.8672,8.2720)  # Lower-left corner
max_lon, max_lat = (77.17,8.54)  # Upper-right corner
bbox = (min_lon, min_lat, max_lon, max_lat)

enter image description here

1 Answers

There may be other, cooler ways to do this. The method I have devised is to create a sequence of 5 equal parts of the least and greatest in latitude and longitude, respectively. I then loop through it by latitude and longitude to find the coordinates of each rectangle. For the coordinates I find, I create a 4x4 using the rectangles.

import numpy as np

min_lon, min_lat = (76.8672,8.2720)  # Lower-left corner
max_lon, max_lat = (77.17,8.54)  # Upper-right corner
#bbox = (min_lon, min_lat, max_lon, max_lat)

lon = np.linspace(min_lon, max_lon, 5)
lat = np.linspace(min_lat, max_lat, 5)

latlons = []
for i in range(len(lat)-1):
    for k in range(len(lon)-1):
        latlons.append((lat[k], lon[i], lat[k+1], lon[i+1]))

import folium

m = folium.Map(location=((min_lat+max_lat)/2,(min_lon+max_lon)/2), zoom_start=11)

for k in latlons:
    folium.Rectangle([(k[0], k[1]), (k[2], k[3])],
                     color='red',
                     fill='pink',
                     fill_opcity=0.5).add_to(m)
m

enter image description here

Related