Esempio n. 1
0
        public ActionResult index()
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();
            JsonResult ret = new JsonResult {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            try
            {
                Response.ContentType = "text/plain";
                Response.Charset     = "utf-8";
                HttpPostedFileBase file     = Request.Files["file"];
                string             filePath = Server.MapPath("\\file") + "\\";

                if (file != null)
                {
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                }
                else
                {
                    dic.Add("success", "false");
                    dic.Add("message", "请选择需要上传的文件");
                    ret.Data = dic;
                    return(ret);
                }

                string fileExtension = Path.GetExtension(file.FileName);
                if (fileExtension.ToUpper() != ".XLS" && fileExtension.ToUpper() != ".XLSX")//&& fileExtension.ToUpper() != ".XLSM" && fileExtension.ToUpper() != ".XLSB")
                {
                    dic.Add("success", "false");
                    dic.Add("message", "上传的文件类型错误");
                    ret.Data = dic;
                    return(ret);
                }

                var path = filePath + file.FileName;
                file.SaveAs(path);
                List <KneeCoordinateModal> list = new List <KneeCoordinateModal>();
                using (FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(f);
                    ISheet       sheet    = workbook.GetSheetAt(0);
                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                    {
                        HSSFRow             r          = (HSSFRow)sheet.GetRow(i);
                        KneeCoordinateModal coordinate = new KneeCoordinateModal
                        {
                            CoordinateX = Convert.ToDouble(r.GetCell(0).ToString()),
                            CoordinateY = Convert.ToDouble(r.GetCell(1).ToString())
                        };
                        list.Add(coordinate);
                    }
                }
                dic.Add("success", "true");
                dic.Add("message", "文件导入成功");
                dic.Add("data", list);
                System.IO.File.Delete(path);
            }
            catch (Exception exception)
            {
                dic = new Dictionary <string, object>();
                dic.Add("success", "false");
                dic.Add("message", exception.Message);
            }
            ret.Data = dic;
            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// 通过转换获得地理坐标列表(经、纬度)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public KneeCoordinateOutput GeoCoordinateList(KneeCoordinateInput input)
        {
            KneeCoordinateOutput output = new KneeCoordinateOutput
            {
                status = false,
                msg    = "数据是空的,坐标转换失败!"
            };

            if (!input.IsProjectiveGeo)//过滤地理坐标的情况
            {
                output.msg  = "已经是地理坐标,不需要转换,仅提供投影转地理坐标的情况!";
                output.data = input.InitialData;
                return(output);
            }

            List <KneeCoordinateModal> list = new List <KneeCoordinateModal>();


            if (input != null && input.InitialData.Count != 0)
            {
                object fromCSType = null;
                object toCSType   = null;

                if (input.CoordSys == "北京54坐标系")
                {
                    toCSType = CSType.GCS_Beijing_1954;
                }
                else if (input.CoordSys == "西安80坐标系")
                {
                    toCSType = CSType.GCS_Xian_1980;
                }
                //else if (input.CoordSys == "国家2000坐标系"):
                //{
                //    toCSType = CSType.GCS_COUNTRY_2000;
                //}
                //else if (input.CoordSys == "WGS84坐标系"):
                //{
                //    toCSType = CSType.GCS_WGS_84;
                //}
                if (toCSType == null)
                {
                    output.msg = "toCSType为null,坐标转换失败!";
                    return(output);
                }

                int i = 1;
                foreach (KneeCoordinateModal item in input.InitialData)
                {
                    if (item.CoordinateY == 0.0)
                    {
                        output.msg  = "第" + i + "行Y坐标值为0,坐标转换失败!";
                        output.data = null;
                        return(output);
                    }
                    fromCSType = SelectCSType(item.CoordinateY, input.CoordSys);
                    if (fromCSType == null)
                    {
                        output.msg  = "第" + i + "行Y坐标值错误导致fromCSType为null,坐标转换失败!";
                        output.data = null;
                        return(output);
                    }
                    var point = CSWKTUtility.TransForm(item.CoordinateX, item.CoordinateY, (CSType)fromCSType, (CSType)toCSType);
                    if (point != null)
                    {
                        KneeCoordinateModal kcm = new KneeCoordinateModal
                        {
                            CoordinateX = point[0],
                            CoordinateY = point[1]
                        };
                        list.Add(kcm);
                    }
                    i++;
                }

                if (list[0] != list[list.Count - 1])
                {
                    //尾巴补齐:首尾坐标要重合
                    list.Add(list[0]);
                }
                output.status = true;
                output.msg    = "坐标转换成功!";
                output.data   = list;
            }
            return(output);
        }