public async Task <IActionResult> DeleteConfirmed(int id)
        {
            LayerRecord layerRecord = await ConfigContext.Layers.FindAsync(id);

            if (layerRecord == null)
            {
                return(NotFound("未找到图层"));
            }
            ServiceRecord serviceRecord = await ConfigContext.Services.FindAsync(layerRecord.ServiceId);

            if (serviceRecord == null)
            {
                return(StatusCode(500));
            }

            string capabilitiesPath = ServicePathManager.GetCapabilitiesPath(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name);
            await OgcServiceHelper.RemoveLayerFromCapabilities(serviceRecord, capabilitiesPath, layerRecord);

            return(RedirectToAction(nameof(Index)));
        }
Exemple #2
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var serviceRecord = await ConfigContext.Services.FindAsync(id);

            if (serviceRecord == null)
            {
                return(BadRequest("未找到要删除的服务"));
            }
            string capabilitiesDirectory = ServicePathManager.GetServiceDirectory(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name);

            if (Directory.Exists(capabilitiesDirectory))
            {
                Directory.Delete(capabilitiesDirectory, true);
            }
            var layerRecords = ConfigContext.Layers.Where(x => x.ServiceId == id);

            ConfigContext.Layers.RemoveRange(layerRecords);
            ConfigContext.Services.Remove(serviceRecord);
            await ConfigContext.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Upload([FromForm] IFormCollection formData)
        {
            string error = null;
            var    files = formData.Files;
            long   size  = files.Sum(f => f.Length);

            if (!Request.Form.ContainsKey("ServiceId"))
            {
                error = "参数未包含ServiceId";
                return(BadRequest(error));
            }
            StringValues values = Request.Form["ServiceId"];

            if (values.Count == 0)
            {
                error = "参数未包含ServiceId";
                return(BadRequest(error));
            }
            bool ret = int.TryParse(values[0], out int serviceId);

            if (!ret)
            {
                error = "ServiceId错误";
                return(BadRequest(error));
            }
            ServiceRecord serviceRecord = await ConfigContext.Services.FindAsync(serviceId);

            if (serviceRecord == null)
            {
                error = "未找到指定的服务";
                return(BadRequest(error));
            }
            string capabilitiesPath = ServicePathManager.GetCapabilitiesPath(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name);

            if (!System.IO.File.Exists(capabilitiesPath))
            {
                error = "服务器内部错误:未找到元数据";
                return(BadRequest(error));
            }
            string tempDirectory = Path.GetTempPath();

            if (!Directory.Exists(tempDirectory))
            {
                Directory.CreateDirectory(tempDirectory);
            }
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string zipNameWithExtension = Path.GetFileName(formFile.FileName);
                    string zipName     = Path.GetFileNameWithoutExtension(formFile.FileName);
                    string tempZipPath = Path.Combine(tempDirectory, zipNameWithExtension);//zip临时路径
                    using (var stream = System.IO.File.Create(tempZipPath))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                    string tempZipDirectory = Path.Combine(tempDirectory, zipName);//zip临时解压目录
                    ZipUtil.UnZip(tempZipPath, tempZipDirectory);
                    List <string> srcFileNames  = GetSupportFileNames(tempZipDirectory);
                    string        destDirectory = ServicePathManager.GetServiceDirectory(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name);
                    if (!Directory.Exists(destDirectory))
                    {
                        Directory.CreateDirectory(destDirectory);
                    }
                    foreach (var srcFileName in srcFileNames)
                    {
                        string destNameWithExtension = Path.GetFileName(srcFileName);
                        string destFileName          = Path.Combine(destDirectory, destNameWithExtension);
                        OgcServiceHelper.MoveDataSet(srcFileName, destFileName);

                        ret = OgcServiceHelper.AddLayerToCapabilities(serviceRecord, capabilitiesPath, destFileName);
                        if (!ret)
                        {
                            OgcServiceHelper.DeleteDataSet(destFileName);
                        }
                    }
                    Directory.Delete(tempZipDirectory, true);
                    System.IO.File.Delete(tempZipPath);
                }
            }
            int result = await ConfigContext.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IActionResult> Preview(int?id, int width, int height)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var layerRecord = await ConfigContext.Layers
                              .Include(l => l.Service)
                              .FirstOrDefaultAsync(m => m.Id == id);

            if (layerRecord == null)
            {
                return(NotFound());
            }
            ServiceRecord serviceRecord = layerRecord.Service;
            View          view          = new View();

            OpenLayers.layer.BaseLayer baseLayer = null;
            switch (serviceRecord.Type)
            {
            case OgcServiceType.Wmts:
                GdalWmtsService gdalWmtsService = new GdalWmtsService();

                string       capabilitiesPath = ServicePathManager.GetCapabilitiesPath(serviceRecord.Type, serviceRecord.Version, serviceRecord.Name);
                Capabilities capabilities     = null;
                using (StreamReader sr = new StreamReader(capabilitiesPath))
                {
                    capabilities = gdalWmtsService.XmlDeSerialize(sr);
                }
                LayerType     layerType             = capabilities.GetLayerType(layerRecord.Name);
                string        format                = layerType.Format.FirstOrDefault();
                string        destTileMatrixSetName = layerType.TileMatrixSetLink.FirstOrDefault()?.TileMatrixSet;
                TileMatrixSet tileMatrixSet         = capabilities.GetTileMatrixSet(destTileMatrixSetName);
                double[]      origin                = tileMatrixSet.TileMatrix.FirstOrDefault()?.TopLeftCorner?.ToDoubleValues();
                string[]      matrixIds             = tileMatrixSet.TileMatrix.Select(x => x.Identifier.Value).ToArray();
                double[]      resolutions           = tileMatrixSet.GetResolutions(matrixIds).ToArray();

                var      bound       = layerType.BoundingBox.FirstOrDefault();
                double[] lowerCorner = bound.LowerCorner.ToDoubleValues();
                double[] upperCorner = bound.UpperCorner.ToDoubleValues();
                double   xmin        = lowerCorner[0];
                double   ymin        = lowerCorner[1];
                double   xmax        = upperCorner[0];
                double   ymax        = upperCorner[1];
                double[] extent      = new double[] { xmin, ymin, xmax, ymax };
                double   centerX     = (xmin + xmax) / 2;
                double   centerY     = (ymin + ymax) / 2;
                view.center        = new double[] { centerX, centerY };
                view.extent        = extent;
                view.resolution    = TileMatrixSet.GetSuitableResolution(resolutions.ToList(), xmin, ymin, xmax, ymax, width, height);
                view.minResolution = resolutions[resolutions.Length - 1];
                view.maxResolution = resolutions[0];
                if (!string.IsNullOrEmpty(tileMatrixSet.SupportedCRS))
                {
                    string[] array = tileMatrixSet.SupportedCRS.Split("EPSG");
                    if (array.Length > 0)
                    {
                        string epsg = array[array.Length - 1].Replace(":", "");
                        view.projection = $"EPSG:{epsg}";
                    }
                }
                WmtsTileGrid tileGrid = new WmtsTileGrid()
                {
                    origin      = origin,
                    matrixIds   = matrixIds,
                    resolutions = resolutions,
                    extent      = extent
                };
                OpenLayers.source.Wmts source = new OpenLayers.source.Wmts()
                {
                    url       = capabilities.GetHref(),
                    layer     = layerRecord.Name,
                    tileGrid  = tileGrid,
                    matrixSet = destTileMatrixSetName,
                    format    = format
                };
                baseLayer = new OpenLayers.layer.TileLayer()
                {
                    source = source
                };
                break;

            default:
                return(NotFound());
            }
            Map map = new Map()
            {
                layers = new OpenLayers.layer.BaseLayer[] { baseLayer },
                view   = view
            };

            return(new JsonResult(map));
            //PreviewModel previewModel = new PreviewModel()
            //{
            //    Layers = new OpenLayers.layer.BaseLayer[] { baseLayer },
            //    View = view
            //};
            //return View(previewModel);
        }
 public BaseController(IHostingEnvironment environment, ConfigContext configContext)
 {
     HostingEnvironment = environment;
     ConfigContext      = configContext;
     ServicePathManager = new ServicePathManager(HostingEnvironment.ContentRootPath, ConfigContext);
 }