views:

1380

answers:

3

I want to create some heat-map style tiles to overlay over our base maps using Open Layers. Basically, I want to divide some some bounding box into a grid, and display each square of the grid using a different color based on how many points of a sample fall within that grid square.

The technologies involved are C#, OpenLayers, SQL Server 2008 and GeoServer.

My question is basically one of general approach, I'm not really sure where to put the tip of the chisel on this one.

My ultimate goal is to be able to take any arbitrary bounding box, calculate an x-mile by x-mile grid that fits within that bounding box, the iterate over a collection of individual points and assign them to one grid square or another so I can calculate point density per grid square, then color the grid according to the densities, then overlay that on a CloudMade base map using Open Layers.

Any help at all would be greatly appreciated, on the whole thing or any piece of it.

+2  A: 

If your bounding box is axis aligned, this is fairly simple. Just make your image, and create a world file for it by hand. The world file is just 6 lines of text, and you already know everything needed (x & y pixel size, coordinate of your upper left corner).

Just make sure that you use the CENTER of the upper left corner pixel, not the corner of the box.

------ Here's how you'd make the world file -------

Say your bounding box's upper left corner is at 203732x598374, and you want an image that has rectangles that are 200m wide east<->west and 300m tall north<->south.

You'd make an image that was the appropriate number of pixels, then a world file that had the following 6 lines:

200
0
0
-300
203632
598524

This corresponds to:

200 == size of one pixel in X
0 == shear1
0 == shear2
-300 == size of one pixel in Y (from top down)
203632 == left edge - 1/2 pixel size (to center on pixel instead of edge of box)
598524 == top edge - 1/2 pixel size (to center on pixel instead of edge of box)

If you use a .png image, you'll want to save this with the same name, but as .pgw. If you use a .jpg, it'd be .jgw, etc.

For complete details, see: Wiki on World Files

Reed Copsey
What do you mean by axis aligned?
Nathan
Axis aligned == your "bounding box" is setup so it is aligned with East and North (aligned with the axes). This makes the above method very easy. If it's not, it's still possible, but the georeferencing is more difficult. Let me know if you need more clarification.
Reed Copsey
By aligned, do you mean that the bottom left corner is X = 0, Y = 0? If so, that won't quite work, as the image I'd generate would then be the entire earth - so I am probably misunderstanding you : )
Nathan
No-just that, as you move along +X, your Y value doesn't change. For example, if the upper left corner of the image is at 1182893,1987317 the upper right corner's Y is at 1987317 too. The left edge's X needs to be constant, and the top edge's Y is constant. (Basically, the rectangle isn't rotated)
Reed Copsey
Does that help, Nathan?
Reed Copsey
Beautiful! Thank you!
Nathan
+2  A: 

"Dividing some some bounding box into a grid, and displaying each square of the grid using a different color based on how many points of a sample fall within that grid square." This is a raster and there are features in GeoServer for displaying these with colour shading, legends and so on. I think it will be more flexible to use these features than to create image tiles in C#.

From the GeoServer documentation:

Raster data is not merely a picture, rather it can be thought of as a grid of georeferenced information, much like a graphic is a grid of visual information (with combination of reds, greens, and blues). Unlike graphics, which only contain visual data, each point/pixel in a raster grid can have lots of different attributes, with possibly none of them having an inherently visual component.

This is also called thematic mapping or contour plots or heatmaps or 2.5D plots in other GIS packages.

You could use a free GIS like Grass to create the raster grids, but from your description you don't need to interpolate (because every cell contains at least one point) so it might be just as easy to roll your own code.

EDIT: there is an open source library GDAL which you can use to write raster files in various formats. There are C# bindings.

MarkJ
Excellent resources, thank you. This is the info I was after.
Nathan
A: 

I think the formulas for computing the center of the upper left pixel are wrong. In the example, the center of the upper left pixel would be down and to the right of (203732,598374). So shouldn't it be the following?

203832 == left edge + 1/2 pixel size (to center on pixel instead of edge of box)
598224 == top edge - 1/2 pixel size (to center on pixel instead of edge of box)