Evaluate geographically distributed data over a polygon

Information on global population is available in gridded format, that is in discrete points spaced in a regular latitude-longitude grid. This information may represent the number of users (people or vehicles) in a quadratic area around the grid point (population density), or certain properties related to the users.
The grid for the data is defined through minimum latitude (latmin), maximum latitude (latmax), step size in latitude (latstep), minimum longitude (lonmin), maximum longitude (lonmax), step size in longitude (lonstep).
The data shall be evaluated across an arbitrarily shaped polygon, representing a region or country. The polygon is defined through a number of points along its edge. The number of users or the properties related to users shall be integrated across the polygon area.
In order to achieve this I want to express the polygon by means of a latitude/longitude matrix, where a 1 is assigned to gridpoints inside (or at edge), and a 0 to gridpoints outside the polygon. The grid needs to be the same as for the user dataset. This polygon matrix POLY can serve as a mask for the dataset DATA. Then I can simply multiply the matrices to achieve a matrix carrying the user data for each point inside, and a 0 for each point outside the polygon.
To generate the polygon I found I can use the poly2mask command. My challenge is that the grid of the resulting matrix is not the same as for the dataset DATA. How can I achieve this?
Is there any alternative way to screen the data through a mask related to a polygon?

 Accepted Answer

I'm not entirely clear on how you define your polygons, I don't understand why you'd want to use poly2mask, but if they are defined as a set of vertices, you can easily create your grid mask with inpolygon:
[latgrid, longrid] = ndgrid(latmin:latsep:latmax, longmin:longstep:longmax);
polygonvertices = [10 2
5 5
10 10
20 10
25 5
20 2]; %for example. rows are vertices, columns are lat/lon
gridmask = inpolygon(latgrid, longrid, polygonvertices(:, 1), polygonvertices(:, 2));
I've used an nvertices x 2 matrix to define the polygon but you can use whatever format you want, as long you pass the coordinates of the vertices to inpolygon.

3 Comments

Many thanks Guillaume for the swift reply! I did a quick test, but got the following error message:
Error using & Inputs must have the same size.
Error in inpolygon (line 76) mask = (x >= min(xv)) & (x max(xv)) & (y=min(yv)) & (y<=max(yv));
It seems the latgrid and longrid vectors need to have the same length, which is not the case in my example. Need to fiddle more with this tomorrow.
D'oh! It's my fault. latgrid and longrid need to be ndgrid'ed or meshgrid'ed. Fixed that in my answer.
Yes, it works now. Many thanks for your help Guillaume!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 21 Jan 2016

Commented:

on 22 Jan 2016

Community Treasure Hunt

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

Start Hunting!