GISjob.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using log4net;
  2. using Quartz;
  3. using SuperMap.Data;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. namespace TimedUpload.QuartzJobs
  12. {
  13. class GISjob : IJob
  14. {
  15. private readonly ILog log = LogManager.GetLogger(typeof(GISjob));
  16. private readonly static string selectGX = "SELECT gx.objectid as pipeLineId,cd as realLength,gx.gj as pipeCaliber,gdbh as pipeLineNo,gx.cz as pipeMaterial,gx.msrq as buidTime,gx.enabled as useState,qsdh,zddh,gdqs.smx as qsdhsmx,gdqs.smy as qsdhsmy,gdzd.smx as zddhsmx,gdzd.smy as zddhsmy from SMDTV_5 as gx INNER JOIN SMDTV_6 as gdqs on gdqs.bsm=gx.qsdh INNER JOIN SMDTV_6 as gdzd on gdzd.bsm=gx.zddh";
  17. public void Execute(IJobExecutionContext context)
  18. {
  19. try
  20. {
  21. GISdata();
  22. }
  23. catch (Exception ex)
  24. {
  25. log.Error("执行Execute异常:" + ex.Message);
  26. }
  27. }
  28. private void GISdata()
  29. {
  30. DataTable drPipeArea = MysqlHelper.Query("select * from bs_pipearea where PipeName='GIS定时任务'").Tables[0];
  31. if (drPipeArea.Rows.Count != 1)
  32. {
  33. MysqlHelper.ExecuteSql("DELETE from bs_pipearea where PipeName='GIS定时任务'");
  34. MysqlHelper.ExecuteSql("INSERT INTO bs_pipearea(PipeAreaId, PipeName) VALUES(1,'GIS定时任务')");
  35. }
  36. MysqlHelper.ExecuteSql("DELETE from bs_pipeline where PipeAreaId=1");
  37. DataTable dt = NpgsqlHelper.ExecuteQuery(selectGX).Tables[0];
  38. PrjCoordSys showPJcoordSys = new PrjCoordSys(PrjCoordSysType.China20003DegreeGk40N);
  39. int insertPipeline = 0;
  40. int baiduAPI = 0;
  41. int chaotuAPI = 0;
  42. Point2Ds point2Ds = new Point2Ds();
  43. foreach (DataRow dr in dt.Rows)
  44. {
  45. point2Ds.Clear();
  46. point2Ds.Add(new Point2D(StringToDouble(dr["qsdhsmx"]), StringToDouble(dr["qsdhsmy"])));
  47. point2Ds.Add(new Point2D(StringToDouble(dr["zddhsmx"]), StringToDouble(dr["zddhsmy"])));
  48. if (CoordSysTranslator.Inverse(point2Ds, showPJcoordSys))
  49. {
  50. string baiduURL = string.Format("https://api.map.baidu.com/geoconv/v1/?coords={0},{1};{2},{3}&from=1&to=5&ak=Gtjd2sKDSMYBSEGqj3KEZaM8axOQgGnA", point2Ds[0].X, point2Ds[0].Y, point2Ds[1].X, point2Ds[1].Y);
  51. HttpWebRequest requestbd = (HttpWebRequest)WebRequest.Create(baiduURL);
  52. requestbd.Method = "GET";
  53. HttpWebResponse responsebd = (HttpWebResponse)requestbd.GetResponse();
  54. Stream responseStreambd = responsebd.GetResponseStream();
  55. StreamReader streamReaderbd = new StreamReader(responseStreambd, Encoding.UTF8);
  56. dynamic responseJson = Newtonsoft.Json.JsonConvert.DeserializeObject(streamReaderbd.ReadToEnd());
  57. if (responseJson.status == 0)
  58. {
  59. //log.Info("坐标转换成功 管线objectid=" + dr["pipeLineId"]);
  60. #region HttpWebRequest
  61. //log.Info(string.Format("成功 坐标由WGS84({0},{1}|{2},{3})转换为BD09II({4},{5}|{6},{7})", point2Ds[0].X, point2Ds[0].Y, point2Ds[1].X, point2Ds[1].Y, responseJson.result[0].x.Value, responseJson.result[0].y.Value, responseJson.result[1].x.Value, responseJson.result[1].y.Value));
  62. //responseJson.result[0].x.Value, responseJson.result[0].y.Value, responseJson.result[1].x.Value, responseJson.result[1].y.Value
  63. //string coordinates = string.Format("{0},{1}|{2},{3}", responseJson.result[0].x.Value, responseJson.result[0].y.Value, responseJson.result[1].x.Value, responseJson.result[1].y.Value);
  64. //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(doURL);
  65. //req.Method = "POST";
  66. //req.ContentType = "application/json";
  67. //string JSONData = string.Format(addPipeLineJsonData, dr["pipeLineId"], dr["realLength"], coordinates);
  68. //byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
  69. //req.GetRequestStream().Write(bytes, 0, bytes.Length);
  70. //HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  71. //Stream streamReceive = rsp.GetResponseStream();
  72. //StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
  73. //dynamic rspJson = Newtonsoft.Json.JsonConvert.DeserializeObject(streamReader.ReadToEnd());
  74. //if (rspJson.success == true)
  75. //{
  76. // log.Info("写入成功 {0}", rspJson.data);
  77. //}
  78. //else
  79. //{
  80. // log.Error("写入失败 {0}", rspJson.errorMessage);
  81. //}
  82. #endregion
  83. string coordinates = string.Format("{0},{1}|{2},{3}", responseJson.result[0].x.Value, responseJson.result[0].y.Value, responseJson.result[1].x.Value, responseJson.result[1].y.Value);
  84. string sql = string.Format("INSERT INTO bs_pipeline(PipeLineId,PipeAreaId,pipeLineNo,realLength,coordinates,pipeCaliber,PipeLineName) VALUES({0},1,'{0}',{1},'{2}',{3},'{4}')", dr["pipeLineId"], dr["realLength"], coordinates, dr["pipeCaliber"], "导入" + dr["pipeLineId"]);
  85. int r = MysqlHelper.ExecuteSql(sql);
  86. if (r == 1)
  87. {
  88. insertPipeline++;
  89. log.Info("写入成功 管线objectid=" + dr["pipeLineId"]);
  90. }
  91. else
  92. {
  93. log.Error("写入失败 管线objectid=" + dr["pipeLineId"]);
  94. }
  95. }
  96. else
  97. {
  98. baiduAPI++;
  99. log.Error("坐标转换失败 管线objectid=" + dr["pipeLineId"]);
  100. }
  101. }
  102. else
  103. {
  104. chaotuAPI++;
  105. log.Info(string.Format("超图接口转换失败,管线objectid={0}", dr["pipeLineId"]));
  106. }
  107. }
  108. log.Info(string.Format("数据总量{0}条,写入成功{1}条,百度坐标转换失败{2}条,超图转换失败{3}条", dt.Rows.Count, insertPipeline, baiduAPI, chaotuAPI));
  109. MysqlHelper.ExecuteSql("UPDATE bs_pipearea set PipeLineNum = " + insertPipeline + " WHERE PipeAreaId = 1");
  110. }
  111. public static double StringToDouble(object str)
  112. {
  113. if (str is null) return 0;
  114. double result = 0.0D;
  115. bool r = double.TryParse(str.ToString(), out result);
  116. return result;
  117. }
  118. }
  119. }