Question

How do I take a set of polygons which contain arbitrary values and create a corresponding bitmap where each pixel contains the value of the polygon at that location?

To put the question into context, my polygons contain information about the average number of people per square kilometre within the polygon. I need to create a raster/bitmap that contains pixels representing the population in 200 metre bins.

I've done something similar in the past where I've used a polygon to create a mask by drawing into a bitmap and filling values, then converting the bitmap into an array that I can manipulate. I'm sure there's a better method for doing this!

I'm clarifying the question a bit more as requested.

  1. There are multiple polygons, each polygon is a set of vectors
  2. Each polygon will have a single unique value
  3. The polygons don't overlap

Thanks

Nick

Was it helpful?

Solution

@Nick R

I was originally using ArcGIS 9.2, but that doesn't work well with C# and 64 bit, so I am now using GDAL (http://www.gdal.org).

Doesn't gdal_rasterize do exactly what you want?

OTHER TIPS

What GIS software are you using? ArcGIS offers the Polygon to Raster tool in ArcGIS 9.2 or later, which is scriptable as the PolygonToRaster_conversion function.

PolygonToRaster_conversion (in_features, value_field, out_raster_dataset, cell_assignment, priority_field, cellsize)

This probably isn't what you need, but if you want to draw a polygon (or conversely read a polygon image's pixels on a polygon basis) then one solution is to roll your own polygon fill tool. Quite frankly, this is a ton of fun, and really neat to learn about.

But your question isn't very clear to me. Can you give a better description?

  • Is your set of arbitrary polygons actual images, or vector (ie, list of points) points, or ???
  • Does each polygon have one value, or does each polygon have an array of values you are trying to draw?
  • So each polygon has an associated array of population values that you want to essentially texture the polygon with?

-Adam

It is a fun project. Here's what I would do, assuming the polygons are convex:

 have a NY * 2 array of x positions: int x[NY][2]
foreach polygon
  clear the array to -1
  for each edge line
    foreach horizontal raster line iy intersecting the line
      generate ix, the x position where the raster intersects the line
      if x[iy][0] == -1, set it to ix, else set x[iy][1] to ix
    end foreach iy
  end foreach edge
  foreach iy
    fill the pixels between x[iy][0] and x[iy][1] with the polygons label
  end foreach iy
end foreach polygon

This is a little more tricky than it sounds, because you need the mental discipline to think of raster coordinates NOT as the pixels to be labeled, but as the invisible lines BETWEEN them. Otherwise, you get all confused by boundary issues.

A good test of this is if you have a polygon of zero area, like it consists of edges from point A to point B and back to A, it should light up no pixels. Another test is if you have a parallelogram that is 2 units high, and it's top and bottom edges are 2 units wide, it should light up exactly 4 pixels.

If the polygons are NOT convex, it's a little different. Wherever an edge crosses a raster line, toggle all the pixels from there to some arbitrarily chosen X coordinate, such as the left edge of the "screen". When you have completed all the edges, only the interior pixels will have been toggled an odd number of times.

ImageMagick can convert from svg to png, maybe you can take a look at the code, or simply create svg and use IM for the conversion? Scruffy does that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top