create grid of squares over map using mapping toolbox

Hi,
I am working on a project where i have specific locations (latitude/longitude) and need to create a type of heat-map which represents how populated each region.
I would like to create a grid of squares over the map (which i have created using the mapping toolbox) and color each square based on the number of points registered inside it. I have done a similar thing using circles, where i verify if the point is inside each circle using the "inpoly" function. However, i am finding it challenging to do the same using squares. I guess as long as i know the corner points of each square, i would still be able to follow the same approach.
I have attached a sample image to show you the type of grid i am looking for. Ideally, i would like to be able to specify the size of each of the squares in for example km. The map was produced using the following code (the grids were overlaid in paint):
figure(1);
f=worldmap([33 68],[-15 37]);
geoshow('landareas.shp', 'FaceColor', [1 1 1],'DefaultEdgeColor', 'b')
Thank you very much for you help in advance.

Answers (1)

You can add a grid like this:
f=worldmap([33 68],[-15 37]);
geoshow('landareas.shp', 'FaceColor', [1 1 1],'DefaultEdgeColor', 'b')
AxisHandle = gca ;
GridHandle = gridxy(linspace(AxisHandle.XLim(1),AxisHandle.XLim(2),25), ...
linspace(AxisHandle.YLim(1),AxisHandle.YLim(2),25)) ;
set(GridHandle,'color',[.6 .6 .6],'LineWidth',.5) ;
uistack(GridHandle,'top')
My function GRIDXY is available at the Matlab File Exchange and can be downloaded here: http://www.mathworks.com/matlabcentral/fileexchange/9973

4 Comments

Hi Jos,
Many thanks for your Answer. However this only adds the grid lines. I was actually looking for a solution that would also provide me with the exact corner points of each of the squares inside the grid, so that i can find which square each of my locations lies within. any suggestions?
The x-coordinates of the lines are given by the expression linspace(AxisHandle.XLim(1),AxisHandle.XLim(2),25)
Is this what you're after?
Jos,
Yes it does provide the points, but i would need to have the coordinates in latitude/longitude. Also, the "25" in that lines specifies the number of squares generated over the plot. However, i need to specify the size of each of the squares instead. so for example, the side length of each square to be 25km.
I am not sure about the units of the axes, but you can create these points using colon notation (start:step:end) So if the units are kilometers, this would do, I think:
Xpoints = AxisHandle.XLim(1):25:AxisHandle.XLim(2)

Sign in to comment.

Asked:

on 1 Mar 2016

Commented:

on 2 Mar 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!