using log4net; using Quartz; using SuperMap.Data; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Text; namespace TimedUpload.QuartzJobs { class GISjob : IJob { private readonly ILog log = LogManager.GetLogger(typeof(GISjob)); 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"; public void Execute(IJobExecutionContext context) { try { GISdata(); } catch (Exception ex) { log.Error("执行Execute异常:" + ex.Message); } } private void GISdata() { DataTable drPipeArea = MysqlHelper.Query("select * from bs_pipearea where PipeName='GIS定时任务'").Tables[0]; if (drPipeArea.Rows.Count != 1) { MysqlHelper.ExecuteSql("DELETE from bs_pipearea where PipeName='GIS定时任务'"); MysqlHelper.ExecuteSql("INSERT INTO bs_pipearea(PipeAreaId, PipeName) VALUES(1,'GIS定时任务')"); } MysqlHelper.ExecuteSql("DELETE from bs_pipeline where PipeAreaId=1"); DataTable dt = NpgsqlHelper.ExecuteQuery(selectGX).Tables[0]; PrjCoordSys showPJcoordSys = new PrjCoordSys(PrjCoordSysType.China20003DegreeGk40N); int insertPipeline = 0; int baiduAPI = 0; int chaotuAPI = 0; Point2Ds point2Ds = new Point2Ds(); foreach (DataRow dr in dt.Rows) { point2Ds.Clear(); point2Ds.Add(new Point2D(StringToDouble(dr["qsdhsmx"]), StringToDouble(dr["qsdhsmy"]))); point2Ds.Add(new Point2D(StringToDouble(dr["zddhsmx"]), StringToDouble(dr["zddhsmy"]))); if (CoordSysTranslator.Inverse(point2Ds, showPJcoordSys)) { 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); HttpWebRequest requestbd = (HttpWebRequest)WebRequest.Create(baiduURL); requestbd.Method = "GET"; HttpWebResponse responsebd = (HttpWebResponse)requestbd.GetResponse(); Stream responseStreambd = responsebd.GetResponseStream(); StreamReader streamReaderbd = new StreamReader(responseStreambd, Encoding.UTF8); dynamic responseJson = Newtonsoft.Json.JsonConvert.DeserializeObject(streamReaderbd.ReadToEnd()); if (responseJson.status == 0) { //log.Info("坐标转换成功 管线objectid=" + dr["pipeLineId"]); #region HttpWebRequest //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)); //responseJson.result[0].x.Value, responseJson.result[0].y.Value, responseJson.result[1].x.Value, responseJson.result[1].y.Value //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); //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(doURL); //req.Method = "POST"; //req.ContentType = "application/json"; //string JSONData = string.Format(addPipeLineJsonData, dr["pipeLineId"], dr["realLength"], coordinates); //byte[] bytes = Encoding.UTF8.GetBytes(JSONData); //req.GetRequestStream().Write(bytes, 0, bytes.Length); //HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); //Stream streamReceive = rsp.GetResponseStream(); //StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8); //dynamic rspJson = Newtonsoft.Json.JsonConvert.DeserializeObject(streamReader.ReadToEnd()); //if (rspJson.success == true) //{ // log.Info("写入成功 {0}", rspJson.data); //} //else //{ // log.Error("写入失败 {0}", rspJson.errorMessage); //} #endregion 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); 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"]); int r = MysqlHelper.ExecuteSql(sql); if (r == 1) { insertPipeline++; log.Info("写入成功 管线objectid=" + dr["pipeLineId"]); } else { log.Error("写入失败 管线objectid=" + dr["pipeLineId"]); } } else { baiduAPI++; log.Error("坐标转换失败 管线objectid=" + dr["pipeLineId"]); } } else { chaotuAPI++; log.Info(string.Format("超图接口转换失败,管线objectid={0}", dr["pipeLineId"])); } } log.Info(string.Format("数据总量{0}条,写入成功{1}条,百度坐标转换失败{2}条,超图转换失败{3}条", dt.Rows.Count, insertPipeline, baiduAPI, chaotuAPI)); MysqlHelper.ExecuteSql("UPDATE bs_pipearea set PipeLineNum = " + insertPipeline + " WHERE PipeAreaId = 1"); } public static double StringToDouble(object str) { if (str is null) return 0; double result = 0.0D; bool r = double.TryParse(str.ToString(), out result); return result; } } }