如何将一组多边形转换为位图

发布于 2024-07-05 05:28:42 字数 344 浏览 8 评论 0原文

如何获取一组包含任意值的多边形并创建相应的位图,其中每个像素包含该位置处的多边形的值?

为了将问题放在上下文中,我的多边形包含有关多边形内每平方公里平均人数的信息。 我需要创建一个栅格/位图,其中包含代表 200 米箱内人口的像素。

我过去做过类似的事情,我使用多边形通过绘制位图并填充值来创建蒙版,然后将位图转换为我可以操作的数组。 我确信有更好的方法来做到这一点!

我按照要求进一步澄清这个问题。

  1. 有多个多边形,每个多边形都是一组向量
  2. 每个多边形都有一个唯一的值
  3. 不重叠

谢谢尼克

多边形

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

比忠 2024-07-12 05:28:42

@尼克R

我最初使用的是 ArcGIS 9.2,但它不适用于 C# 和 64 位,因此我现在使用 GDAL (http://www.gdal.org)。

gdal_rasterize 不正是您想要的吗?

@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?

甜尕妞 2024-07-12 05:28:42

您使用什么 GIS 软件? ArcGIS 在 ArcGIS 9.2 或更高版本中提供了“面转栅格”工具,可将其编写为 PolygonToRaster_conversion 函数。

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

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)
寄居人 2024-07-12 05:28:42

这可能不是您所需要的,但是如果您想绘制多边形(或者相反地在多边形的基础上读取多边形图像的像素),那么一种解决方案是滚动您自己的 多边形填充工具。 坦率地说,这非常有趣,而且非常值得学习。

但你的问题对我来说不是很清楚。 你能给出更好的描述吗?

  • 您的一组任意多边形是实际图像,还是矢量(即点列表)点,还是???
  • 每个多边形是否都有一个值,或者每个多边形是否都有一组您要绘制的值?
  • 那么每个多边形都有一个关联的人口值数组,您想用它来对多边形进行基本纹理处理吗?

-亚当

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

任性一次 2024-07-12 05:28:42

这是一个有趣的项目。 假设多边形是凸的,这就是我要做的:

 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

这比听起来更棘手,因为您需要心理训练来将光栅坐标视为不是要标记的像素,而是它们之间的不可见线。 否则,你会因边界问题而感到困惑。

对此的一个很好的测试是,如果您有一个面积为零的多边形,就像它由从 A 点到 B 点再回到 A 的边缘组成,它应该不会点亮任何像素。 另一个测试是,如果您有一个 2 个单位高的平行四边形,并且其顶部和底部边缘均为 2 个单位宽,则它应该恰好亮起 4 个像素。

如果多边形不是凸的,那就有点不同了。 只要边缘与光栅线交叉,就将所有像素从那里切换到某个任意选择的 X 坐标,例如“屏幕”的左边缘。 当您完成所有边缘时,只有内部像素将被切换奇数次。

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.

凉宸 2024-07-12 05:28:42

ImageMagick可以从svg转换为png,也许你可以看一下代码,或者简单地创建svg并使用IM进行转换? Scruffy 就是这样做的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文