<p>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?</p>

<p>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.</p>

<p>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!</p>

<p>I'm clarifying the question a bit more as requested.</p>

<ol>
<li>There are multiple polygons, each polygon is a set of vectors</li>
<li>Each polygon will have a single unique value</li>
<li>The polygons don't overlap</li>
</ol>

<p>Thanks</p>

<p>Nick</p>

[{'answer_id': 58733, 'author': 'Matthias Winkelmann', 'author_id': 4494, 'author_profile': 'https://Stackoverflow.com/users/4494', 'pm_score': 1, 'selected': False, 'text': '<p>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? <a href="http://scruffy.rubyforge.org/" rel="nofollow noreferrer" title="Scruffy">Scruffy</a> does that.</p>\n'}, {'answer_id': 58793, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>PolygonToRaster_conversion (in_features, value_field, out_raster_dataset, cell_assignment, priority_field, cellsize)\n</code></pre>\n'}, {'answer_id': 58805, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 2, 'selected': False, 'text': '<p>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 <a href="http://www.cs.rit.edu/~icss571/filling/index.html" rel="nofollow noreferrer">polygon fill tool</a>.  Quite frankly, this is a ton of fun, and really neat to learn about.</p>\n\n<p>But your question isn\'t very clear to me.  Can you give a better description?</p>\n\n<ul>\n<li>Is your set of arbitrary polygons actual images, or vector (ie, list of points) points, or ???</li>\n<li>Does each polygon have one value, or does each polygon have an array of values you are trying to draw?</li>\n<li>So each polygon has an associated array of population values that you want to essentially texture the polygon with?</li>\n</ul>\n\n<p>-Adam</p>\n'}, {'answer_id': 58835, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 2, 'selected': True, 'text': '<p>@Nick R</p>\n\n<blockquote>\n  <p>I was originally using ArcGIS 9.2, but that doesn\'t work well with C# and 64 bit, so I am now using GDAL (<a href="http://www.gdal.org" rel="nofollow noreferrer">http://www.gdal.org</a>).</p>\n</blockquote>\n\n<p>Doesn\'t <a href="http://www.gdal.org/gdal_rasterize.html" rel="nofollow noreferrer">gdal_rasterize</a> do exactly what you want?</p>\n'}, {'answer_id': 799493, 'author': 'Mike Dunlavey', 'author_id': 23771, 'author_profile': 'https://Stackoverflow.com/users/23771', 'pm_score': 2, 'selected': False, 'text': '<p>It is a fun project. Here\'s what I would do, assuming the polygons are convex:</p>\n\n<pre><code> have a NY * 2 array of x positions: int x[NY][2]\nforeach polygon\n  clear the array to -1\n  for each edge line\n    foreach horizontal raster line iy intersecting the line\n      generate ix, the x position where the raster intersects the line\n      if x[iy][0] == -1, set it to ix, else set x[iy][1] to ix\n    end foreach iy\n  end foreach edge\n  foreach iy\n    fill the pixels between x[iy][0] and x[iy][1] with the polygons label\n  end foreach iy\nend foreach polygon\n</code></pre>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/58638', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5932/']