You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
9.1KB

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace GpsCardGatewayPosition.Common.Helper
  8. {
  9. public class ChinaPolygon
  10. {
  11. private static readonly PolyGon China;
  12. private const string ChinaStr = "{\"myPts\":[{\"X\":17.978733095556183,\"Y\":108.10546875},{\"X\":12.554563528593656,\"Y\":111.09374999999999},{\"X\":5.8783321096743268,\"Y\":110.390625},{\"X\":6.4026484059638964,\"Y\":115.13671875},{\"X\":11.178401873711785,\"Y\":117.59765625},{\"X\":15.284185114076445,\"Y\":118.828125},{\"X\":20.715015145512098,\"Y\":119.00390625},{\"X\":23.160563309048314,\"Y\":118.740234375},{\"X\":23.704894502324912,\"Y\":118.66333007812499},{\"X\":25.259600649162689,\"Y\":120.42663574218749},{\"X\":30.789036751261161,\"Y\":123.837890625},{\"X\":35.227672354935862,\"Y\":120.9814453125},{\"X\":37.142803443716836,\"Y\":123.53027343749999},{\"X\":39.639537564366684,\"Y\":124.1455078125},{\"X\":40.426042128264932,\"Y\":124.81018066406249},{\"X\":41.029643387166381,\"Y\":126.1285400390625},{\"X\":41.722130585125782,\"Y\":126.826171875},{\"X\":41.516803958101178,\"Y\":127.19970703125},{\"X\":41.335575973123895,\"Y\":127.76000976562501},{\"X\":41.53325414281322,\"Y\":128.441162109375},{\"X\":41.943148747326958,\"Y\":128.265380859375},{\"X\":41.975827261025728,\"Y\":129.0234375},{\"X\":42.366661663732742,\"Y\":129.781494140625},{\"X\":42.89206418807337,\"Y\":130.023193359375},{\"X\":42.455887641971657,\"Y\":130.4296875},{\"X\":42.89206418807337,\"Y\":131.2646484375},{\"X\":43.46886761482925,\"Y\":131.572265625},{\"X\":44.855868807357247,\"Y\":131.28662109375},{\"X\":45.120052841530544,\"Y\":131.9677734375},{\"X\":44.902577996288876,\"Y\":132.8466796875},{\"X\":45.073520606709707,\"Y\":133.30810546875},{\"X\":48.400032496106853,\"Y\":135.19775390625},{\"X\":48.560249791743288,\"Y\":134.89013671875},{\"X\":48.487486479884147,\"Y\":134.09912109375},{\"X\":47.872143968887308,\"Y\":131.46240234375},{\"X\":48.122101028190805,\"Y\":130.89111328125},{\"X\":48.603857608232524,\"Y\":130.9130859375},{\"X\":48.908059399650078,\"Y\":130.75927734375},{\"X\":49.023461463214126,\"Y\":130.58349609375},{\"X\":49.095452162534826,\"Y\":130.23193359375},{\"X\":49.653404588437894,\"Y\":129.5068359375},{\"X\":49.781264058178365,\"Y\":128.056640625},{\"X\":53.146770330850842,\"Y\":126.16699218749999},{\"X\":53.657661020298,\"Y\":123.79394531249999},{\"X\":53.514184520771131,\"Y\":120.89355468749999},{\"X\":52.776185688961711,\"Y\":119.81689453125},{\"X\":52.429222277955134,\"Y\":119.94873046875},{\"X\":52.482780222078226,\"Y\":120.52001953124999},{\"X\":49.710272582105695,\"Y\":117.79541015625001},{\"X\":50.233151832472245,\"Y\":116.16943359374999},{\"X\":48.034019158642863,\"Y\":115.26855468749999},{\"X\":47.502358951968574,\"Y\":115.8837890625},{\"X\":47.694974341862817,\"Y\":118.54248046874999},{\"X\":46.815098645992428,\"Y\":119.68505859375},{\"X\":46.98025235521883,\"Y\":118.63037109375},{\"X\":46.709735944071568,\"Y\":117.3779296875},{\"X\":46.468132992155539,\"Y\":116.5869140625},{\"X\":45.552525251340128,\"Y\":115.31249999999999},{\"X\":45.552525251340128,\"Y\":114.60937499999999},{\"X\":44.840290651397993,\"Y\":113.44482421875},{\"X\":45.243953422623242,\"Y\":111.99462890625},{\"X\":45.073520606709707,\"Y\":111.59912109375},{\"X\":44.308126684886126,\"Y\":111.07177734375},{\"X\":43.78695837311561,\"Y\":111.73095703125},{\"X\":42.8115217450979,\"Y\":109.9951171875},{\"X\":42.585444257384907,\"Y\":107.55615234375},{\"X\":41.787697005390633,\"Y\":104.9853515625},{\"X\":42.779275360241904,\"Y\":100.634765625},{\"X\":42.908160071960538,\"Y\":96.5478515625},{\"X\":44.370986962971728,\"Y\":95.51513671875},{\"X\":44.386691502152061,\"Y\":95.09765625},{\"X\":45.104546309768729,\"Y\":93.40576171875},{\"X\":45.305802599435779,\"Y\":91.20849609375},{\"X\":45.828799251921339,\"Y\":90.94482421875},{\"X\":46.815098645992428,\"Y\":91.16455078125},{\"X\":48.048709942886859,\"Y\":90.175781249999986},{\"X\":48.1367666796927,\"Y\":89.208984374999986},{\"X\":49.210420445650286,\"Y\":87.890625},{\"X\":49.23912083246698,\"Y\":86.7919921875},{\"X\":48.661942846070062,\"Y\":86.46240234375},{\"X\":48.472921272487824,\"Y\":85.62744140625},{\"X\":48.10743118848039,\"Y\":85.341796875},{\"X\":47.189712464484209,\"Y\":85.4296875},{\"X\":47.055154085503482,\"Y\":85.0341796875},{\"X\":47.428087261714253,\"Y\":82.96875},{\"X\":45.767522962149904,\"Y\":82.067871093749986},{\"X\":45.352145245851773,\"Y\":82.309570312499986},{\"X\":45.475540271585928,\"Y\":81.80419921875},{\"X\":44.980342380849727,\"Y\":79.73876953125},{\"X\":43.277205322120238,\"Y\":80.617675781249986},{\"X\":43.100982876188546,\"Y\":80.26611328125},{\"X\":42.924251753870685,\"Y\":80.31005859375},{\"X\":42.763145866894916,\"Y\":80.09033203125},{\"X\":42.163403424224008,\"Y\":80.04638671875},{\"X\":41.11246878918088,\"Y\":77.40966796875},{\"X\":41.1290213474951,\"Y\":76.79443359375},{\"X\":40.547200234410489,\"Y\":76.35498046875},{\"X\":40.480381429081717,\"Y\":75.893554687499986},{\"X\":40.780541431860307,\"Y\":75.5419921875},{\"X\":40.597270634420269,\"Y\":74.8388671875},{\"X\":40.128491056854081,\"Y\":73.8720703125},{\"X\":39.453161128073937,\"Y\":73.49853515625},{\"X\":38.496593518947556,\"Y\":73.6962890625},{\"X\":38.324420427006515,\"Y\":74.5751953125},{\"X\":37.370157184057533,\"Y\":74.8828125},{\"X\":37.247821201554281,\"Y\":74.267578125},{\"X\":35.514343134318182,\"Y\":76.44287109375},{\"X\":35.209721645221386,\"Y\":77.87109375},{\"X\":34.234512362369841,\"Y\":78.37646484375},{\"X\":32.639374873606691,\"Y\":79.12353515625},{\"X\":32.824211101613358,\"Y\":78.662109375},{\"X\":32.620870183181133,\"Y\":78.28857421875},{\"X\":31.184609135743251,\"Y\":78.59619140625},{\"X\":29.821582720575016,\"Y\":81.23291015625},{\"X\":30.12612436422458,\"Y\":81.73828125},{\"X\":28.091366281406945,\"Y\":85.2978515625},{\"X\":27.897349229684259,\"Y\":85.67138671875},{\"X\":27.741884632507087,\"Y\":88.57177734375},{\"X\":27.117812842321225,\"Y\":88.835449218749986},{\"X\":28.013801376380712,\"Y\":89.802246093749986},{\"X\":27.68352808378776,\"Y\":91.69189453125},{\"X\":27.761329874505233,\"Y\":92.74658203125},{\"X\":29.0945770775118,\"Y\":94.72412109375},{\"X\":29.171348850951507,\"Y\":96.04248046875},{\"X\":27.9361805667694,\"Y\":96.9873046875},{\"X\":28.052590823339859,\"Y\":97.97607421875},{\"X\":27.293689224852407,\"Y\":98.26171875},{\"X\":27.371767300523047,\"Y\":98.54736328125},{\"X\":25.839449402063185,\"Y\":98.28369140625},{\"X\":25.165173368663929,\"Y\":97.55859375},{\"X\":23.845649887659352,\"Y\":97.5146484375},{\"X\":23.845649887659352,\"Y\":98.54736328125},{\"X\":21.922663209325936,\"Y\":99.16259765625},{\"X\":21.207458730482653,\"Y\":100.8544921875},{\"X\":21.391704731036587,\"Y\":101.00830078125},{\"X\":21.043491216803556,\"Y\":101.7333984375},{\"X\":22.289096418723052,\"Y\":101.90917968749999},{\"X\":22.532853707527117,\"Y\":102.67822265625},{\"X\":22.816694126899844,\"Y\":105.77636718749999},{\"X\":22.61401087437029,\"Y\":106.50146484374999},{\"X\":21.6778482933475,\"Y\":106.85302734374999},{\"X\":21.371244370618321,\"Y\":108.10546875},{\"X\":20.694461597907797,\"Y\":108.43505859374999},{\"X\":17.978733095556183,\"Y\":108.10546875}]}";
  13. static ChinaPolygon()
  14. {
  15. China = JsonConvert.DeserializeObject<PolyGon>(ChinaStr);
  16. }
  17. public static bool IsInChina(double lat, double lng)
  18. {
  19. return China.FindPoint(lat, lng);
  20. }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. private class Point
  25. {
  26. public double X { get; set; } = 0;
  27. public double Y { get; set; } = 0;
  28. }
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. private class PolyGon
  33. {
  34. public List<Point> myPts = new List<Point>();
  35. public PolyGon()
  36. {
  37. }
  38. public PolyGon(List<Point> points)
  39. {
  40. foreach (Point p in points)
  41. {
  42. myPts.Add(p);
  43. }
  44. }
  45. public void Add(Point p)
  46. {
  47. myPts.Add(p);
  48. }
  49. private int Count()
  50. {
  51. return myPts.Count;
  52. }
  53. // The function will return true if the point x,y is inside the polygon, or
  54. // false if it is not. If the point is exactly on the edge of the polygon,
  55. // then the function may return true or false.
  56. public bool FindPoint(double X, double Y)
  57. {
  58. int sides = Count() - 1;
  59. int j = sides - 1;
  60. bool pointStatus = false;
  61. for (int i = 0; i < sides; i++)
  62. {
  63. if (myPts[i].Y < Y && myPts[j].Y >= Y || myPts[j].Y < Y && myPts[i].Y >= Y)
  64. {
  65. if (myPts[i].X + (Y - myPts[i].Y) / (myPts[j].Y - myPts[i].Y) * (myPts[j].X - myPts[i].X) < X)
  66. {
  67. pointStatus = !pointStatus;
  68. }
  69. }
  70. j = i;
  71. }
  72. return pointStatus;
  73. }
  74. }
  75. }
  76. }