2

Google has a demo for Google Maps which allows you to draw a search area on a page as a free-form polygon, but given a set of entries in my SQL database (~25,000), how could I actually find out which entries are within this shape?

Google's demo: http://gmaps-samples.googlecode.com/svn/trunk/poly/mymapstoolbar.html

You can see this function in action here: http://www.rightmove.co.uk/draw-a-search.html

Maths is not my strong point, so I'm wondering if there is an easy way to do this (preferably at database level) or otherwise in C#?

Edit: The difficult part is doing it for latitude and longitude coordinates (eg, +40.689060, -74.044636) where both the data and the polygon points are in this format. I've found some samples for doing this on a simple x/y plane but doing this on WGS84 lat/long seems like it's going to be very tricky - yet several sites seem to do this! Any accepted answer would need to be for lat/long map coordinates as I know how to solve the problem for a simple x/y grid.

Thanks!

NickG
  • 9,315
  • 16
  • 75
  • 115

2 Answers2

2

I don't know the details of the google api, but have seen and used the rightmove functionality (just recently!). I would suggest something in C# and there are standard algorithms for doing point in polygon checks. In the case of 25,000 items I'd recommend doing them in Parallel otherwise processing could take an age.

StackOverflow - Point in Polygon C#
WikiPedia Point in Polygon Algorithms

Community
  • 1
  • 1
Ian
  • 33,605
  • 26
  • 118
  • 198
  • Thanks, but that's for simple x,y coordinates rather than map coordinates. I'd better update my question to clarify :) – NickG May 01 '12 at 10:57
  • @NickG: There's no difference, apart from a decimal point. Multiply them all by 100,000 if you want to work in integers. I wrote my own point in polygon routine at the previous firm I worked at which was using map co-ordinates I believe in both national grid and lat/long formats. – Ian May 01 '12 at 11:05
  • Fair point - I just wasn't sure all algorithms would handle things like negative values or wrapping between + and - values on the globe. – NickG May 01 '12 at 13:13
  • wrapping between + and - it probably won't handle. – Ian May 01 '12 at 15:14
  • Could be a problem as the UK is on the Greenwich Meridian (0 degrees). – NickG May 01 '12 at 18:34
1

The .Net topology suite http://code.google.com/p/nettopologysuite/ can be used for these calculations. It uses the .Net implementation of GeoAPI http://geoapi.codeplex.com/

Andrew Jones
  • 1,001
  • 1
  • 13
  • 25