// from http://dominoc925.blogspot.com/2012/02/c-code-snippet-to-determine-if-point-is.html public static bool IsPointInPolygon(CoordinatePair[] polygon, CoordinatePair point) { bool isInside = false; for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++) { if (((polygon[i].Y > point.Y) != (polygon[j].Y > point.Y)) && (point.X < (polygon[j].X - polygon[i].X) * (point.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) + polygon[i].X)) { isInside = !isInside; } } return isInside; }
public void CreatePoints(CoordinatePair[] polygon, int[] featureValues) { if (featureValues.Length != features.Length) throw new ArgumentException("featureValues"); var bounds = getBounds(polygon); int total = featureValues.Sum(); Console.WriteLine("Total dots: {0}", total); for (int k=0;k<features.Length;k++) { for (int i = 0; i < featureValues[k]; i++) { if (i % 10000 == 0) Console.Write("."); CoordinatePair point = null; while (true) { point = new CoordinatePair() { X = createRandomValue(bounds[0], bounds[2]), Y = createRandomValue(bounds[1], bounds[3]) }; if (Utils.IsPointInPolygon(polygon, point)) break; } var mPoint = merc.LatLonToMeters(point.Y, point.X); var tileAddress = merc.MetersToTile(mPoint.X, mPoint.Y, 21); var quadkey = merc.QuadTree(tileAddress.X, tileAddress.Y, 21); var person = new Person() { Pos = mPoint, FeatureIndex = k }; people.Add(Tuple.Create(quadkey, person)); } } Console.WriteLine(""); }
public CoordinatePair PixelsToMeters(double px, double py, int zoom) { CoordinatePair retval = new CoordinatePair(); try { var res = Resolution(zoom); retval.X = px * res - this.originShift; retval.Y = py * res - this.originShift; return retval; } catch (Exception ex) { throw ex; } }
public CoordinatePair MetersToPixels(double mx, double my, int zoom) { CoordinatePair retval = new CoordinatePair(); try { var res = Resolution(zoom); retval.X = (mx + this.originShift) / res; retval.Y = (my + this.originShift) / res; return retval; } catch (Exception ex) { throw ex; } }
public CoordinatePair MetersToLatLon(double mx, double my) { CoordinatePair retval = new CoordinatePair(); try { retval.X = (mx / this.originShift) * 180.0; retval.Y = (my / this.originShift) * 180.0; retval.Y = 180 / Math.PI * (2 * Math.Atan(Math.Exp(retval.Y * Math.PI / 180.0)) - Math.PI / 2.0); return retval; } catch (Exception ex) { throw ex; } }
public CoordinatePair LatLonToMeters(double lat, double lon) { CoordinatePair retval = new CoordinatePair(); try { retval.X = lon * this.originShift / 180.0; retval.Y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360.0)) / (Math.PI / 180.0); retval.Y *= this.originShift / 180.0; return retval; } catch (Exception ex) { throw ex; } }
protected double[] getBounds(CoordinatePair[] polygon) { double ll = Double.MaxValue; double bb = Double.MaxValue; double rr = Double.MinValue; double tt = Double.MinValue; foreach (CoordinatePair p in polygon) { ll = Math.Min(ll, p.X); rr = Math.Max(rr, p.X); bb = Math.Min(bb, p.Y); tt = Math.Max(tt, p.Y); } return new double[] { ll, bb, rr, tt }; }