admin管理员组文章数量:1320661
I have a map that I converted from a raster graphic into an SVG file by converting the differently coloured areas into paths.
I know how to do a basic point-in-polygon check given an array of edges, but the svg:path
elements represent multiple polygons as well as masks (to account for seas etc) and extracting that information by parsing the d
attribute seems rather heavy-handed.
Is there a JS library that allows me to simplify that check? I basically want to create random points and then check whether they are on land (i.e. inside the polygons) or water (i.e. outside).
As SVG elements seem to allow for mouse event handling, I would think that this shouldn't be much of a problem (i.e. if you can tell whether the mouse pointer is on top of an element, you are already solving the point-in-polygon problem).
EDIT: Complicating the matter a bit, I should mention that the svg:path
elements seem to be based on curves rather than lines, so just parsing the d
attribute to create an array of edges doesn't seem to be an option.
As the elements can take a fill
attribute, a ghetto approach of rendering the SVG on a canvas and then finding the colour value of the pixel at the given point could work, but that seems like a really, really awful way to do it.
I have a map that I converted from a raster graphic into an SVG file by converting the differently coloured areas into paths.
I know how to do a basic point-in-polygon check given an array of edges, but the svg:path
elements represent multiple polygons as well as masks (to account for seas etc) and extracting that information by parsing the d
attribute seems rather heavy-handed.
Is there a JS library that allows me to simplify that check? I basically want to create random points and then check whether they are on land (i.e. inside the polygons) or water (i.e. outside).
As SVG elements seem to allow for mouse event handling, I would think that this shouldn't be much of a problem (i.e. if you can tell whether the mouse pointer is on top of an element, you are already solving the point-in-polygon problem).
EDIT: Complicating the matter a bit, I should mention that the svg:path
elements seem to be based on curves rather than lines, so just parsing the d
attribute to create an array of edges doesn't seem to be an option.
As the elements can take a fill
attribute, a ghetto approach of rendering the SVG on a canvas and then finding the colour value of the pixel at the given point could work, but that seems like a really, really awful way to do it.
-
After having worked through the SVG spec and spent an hour with Chrome's JavaScript console it seems that the biggest problem is that I have an
svg:path
element rather than a regular shape element. Otherwise it might be possible to usesvg.checkIntersection
with a 1x1svg:rect
(assuming it works for areas rather than outlines). The SVG API seems to be of little help if you're looking for any form of abstraction. – Alan Plum Commented Nov 13, 2010 at 23:59
2 Answers
Reset to default 4The answers on Hit-testing SVG shapes? may help you in this quest. There are issues with missing browser support, but you could perhaps use svgroot.checkIntersection to hit test a small (perhaps even 0 width/height would work?) rectangle within your polygon shape.
The approach I suggested as a last resort seems to be the easiest solution for this problem.
I found a nice JS library that makes it easy to render SVG on a canvas. With the SVG rendered, all it takes is a call to the 2D context's getImageData
method for a 1x1 region at the point you want to check. I guess it helps to create a copy of the SVG with colour coding to make the check easier if your SVG is more plex than the one I'm using (you'll have to check the RGBA value byte-by-byte).
This feels terribly hackish as you're actually inspecting the pixels of a raster image, but the performance seems to be decent enough and the colour checks can be written in a way that allows for impurities (e.g. near the edges).
I guess if you want relative coordinates you could try creating a 1-to-1 sized canvas and then divide the pixel coordinates by the canvas dimensions.
If somebody es up with a better answer, I'll accept it instead. Until then, this one serves as a placeholder in case someone es here with the same problem looking for an easy solution.
本文标签: Point in Polygon check with SVG and JavaScriptStack Overflow
版权声明:本文标题:Point in Polygon check with SVG and JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064163a2418731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论