Find the given Point lie inside a polygon in serverside C#

2,851 views
Skip to first unread message

deep123

unread,
Sep 6, 2012, 5:23:37 AM9/6/12
to google-map...@googlegroups.com


Hello,
 
 I am creating a geofence as a polygon with multiple Poly coordinate (not fixed the poly coordinates) on a map and saving its coordinates on database. Now i am tracking a path with current latitude longitude using GPS . Till here everything is working fine. Now i have to find out that Current latitude longitude is lying inside a polygon or not .  I need the code in server side i can't use java script. I am using google map api v3.



Please anyone answer to my query.

Thanks,

Andrew Leach

unread,
Sep 6, 2012, 5:43:42 AM9/6/12
to google-map...@googlegroups.com
On 6 September 2012 10:23, deep123 <cooldeepi...@gmail.com> wrote:
>
> I am creating a geofence as a polygon with multiple Poly coordinate (not
> fixed the poly coordinates) on a map and saving its coordinates on database.
> Now i am tracking a path with current latitude longitude using GPS . Till
> here everything is working fine. Now i have to find out that Current
> latitude longitude is lying inside a polygon or not . I need the code in
> server side i can't use java script. I am using google map api v3.

I used solution 1 at http://paulbourke.net/geometry/insidepoly/

It's in C, so porting to C# might be possible. I ported to PHP.

deep123

unread,
Sep 6, 2012, 5:55:31 AM9/6/12
to google-map...@googlegroups.com, andrew....@gmail.com


Please Provide me the code in C# . It's urgent.

Thanks.

Andrew Leach

unread,
Sep 6, 2012, 6:18:01 AM9/6/12
to deep123, google-map...@googlegroups.com
On 6 September 2012 10:55, deep123 <cooldeepi...@gmail.com> wrote:
>
>
> Please Provide me the code in C# . It's urgent.

You don't seem to realise the nature of this group. It's a group of
users of Google Maps users who can provide help for each other.

I'm afraid I don't have C# code; I have pointed you to a resource for
server-side point-in-polygon analysis so you can write your own. I'm
not a C# coder and I don't think I've ever even seen C# code.

I suggest you examine the code and start writing it yourself. I can't
imagine it's an invalid start point.

Michael Geary

unread,
Sep 6, 2012, 2:46:19 PM9/6/12
to google-map...@googlegroups.com
The code is very simple and uses language features that are common to most C-like languages. It is easy to port it to C#, Java, JavaScript, Python, Ruby, Go, you name it.

If you want code already written in C#, that's easy to find too:


-Mike

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/aoBYf01Ln9sJ.

To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Blagoja Chavkoski

unread,
Sep 7, 2012, 5:03:40 AM9/7/12
to google-map...@googlegroups.com
Just small thinking-question on the topic. All this suggested solutions are related to 2D space and because here we talk for a maps bounds(circles, square, polygon...) which are all in is a spherical space (ellipsoid to be correct but is count as a normal spherical), so the 2D solutions will be close to correct on a very, very small distances(this can be counted as a plain 2d) but Im pretty sure that the calculus would fall very fast with its correctness.

I havent went that dip in the problem for a polygon, but couple of years ago I implemented this kind of rec for a circle bound in java, and I can say its pretty different.

-Blaze

Rossko

unread,
Sep 7, 2012, 12:41:31 PM9/7/12
to Google Maps JavaScript API v3
> so the 2D solutions will be
> close to correct

You'd have to define what you would call a "correct" 3D solution.
Imagine a circle drawn on a sphere with a point marked within it. Is
the point inside the circle or not from your viewpoint? It is above
the plane of the circle, so could be considered to be 'outside' in 3D
terms.
In spherical geometry (which is indeed 2D) it would be considered
inside, as we are only interested in the surface of the sphere.

Blagoja Chavkoski

unread,
Sep 7, 2012, 2:15:18 PM9/7/12
to google-map...@googlegroups.com
Well my question was more related to people who have used this 2D calculation,
and what are the resoults there getting? Dose actully created polygon from a latlng(s)
and a given random point(latlng) returns correct outcome. I dont know how to use this methods as can be seen all are taking a int values for the poly points, but we have angles in the case of lat lng...


>> In spherical geometry (which is indeed 2D)

Sperical forms are 3D objects and only a 3D objects, in this kind of objects the values changes per 3 axes but this is found to be not important for a proper Sperical object(a ball) because the R stays all the time the same. For the Earth this R= 6371000 so the only important stuff for positioning are the angles which are showing the exact position on the surface.
There values  moves from -90 to 90 and -180 to 180.

So if we take the example that you said a 'circle' on a sphere, teoreticaly there isnt this kind of circle, because this is a part of the sphere that looks like eg baseboll hat, and I dont know how is called in english because its not my native language. But lets take this as a examle the head is the Earth then the hat on the head is a "circle". So we all here ask the question, dose a point with a given latlng is "inside" this circle, but the real question is dose this point lays out on the surface of the head which is overlaped with the hat.

As an example if we take a distance between 2 points on the surface:
     this would be the correct 3D formula:
 
     distance = Math.acos(latPoint1) * Math.sin(latPoint2) +
                Math.cos(latPoint1) * Math.cos(latPoint2) * Math.cos(lngPoint1 - lngPoint2)) * EARTH_RADIUS;
 
  - counting 2D space will have to use the very famous pitagora teorema

So there is a big difference.
       
Also in the js maps libs there is a pakage called geometry and you can see most of the methods are getting a  EARTH_RADIUS as a optional parameter so minning this is used inside, and all the solution from the link(2D) uses just normal int x,y values for the points.

google.maps.geometry.spherical namespace

 

Andrew C Leach

unread,
Sep 7, 2012, 2:54:49 PM9/7/12
to google-map...@googlegroups.com
On 7 September 2012 19:15, Blagoja Chavkoski <baz...@gmail.com> wrote:
> Well my question was more related to people who have used this 2D
> calculation,
> and what are the resoults there getting? Dose actully created polygon from a
> latlng(s)
> and a given random point(latlng) returns correct outcome.

2D and 3D is largely irrelevant because the line-segments are
generally short enough that a straight line between two projected
points is the same as a projected great-circle line between the
points.

If you are attempting to find whether a particular point is within the
Bermuda triangle, it might become relevant (because the sides are
long) but for most polygons it's not because each line defining the
polygon is fairly short.

I've never had any issues with my implementation of the algorithm I
linked to; but I'm using it to determine points in polygons where the
line segments are of the order of metres in length rather than
hundreds of kilometres.

Blagoja Chavkoski

unread,
Sep 7, 2012, 4:08:29 PM9/7/12
to google-map...@googlegroups.com
That was my point. Im not sure till what distance this will be the correct way, but if you are saying that this works pretty ok for "small"
distances then thats a way to go...

I have one question, what are you taking as a x,y values of a point?

In a caucasian system the values would be:

x = r*cos(radians(lng))*cos(radians(lat))
y = r*sin(radians(lng))*cos(radians(lat))
z = r*sin(radians(lat))

in this way we then dont need the radius, so r=1
and the values become

x = cos(radians(lng))*cos(radians(lat))
y = sin(radians(lng))*cos(radians(lat))

Andrew Leach

unread,
Sep 7, 2012, 5:13:14 PM9/7/12
to google-map...@googlegroups.com
On 7 September 2012 21:08, Blagoja Chavkoski <baz...@gmail.com> wrote:
>
> I have one question, what are you taking as a x,y values of a point?

For the algorithm I linked to, and with short side-lengths, it's
sufficient to use the lat/long of the point itself.
Reply all
Reply to author
Forward
0 new messages