Beispiel #1
0
        public JsonNetResult UploadMapDataFile(int?coordinateSystemId)
        {
            JsonModel jsonModel;

            if (Request.Files.Count == 0)
            {
                jsonModel = JsonModel.CreateFailure("No files uploaded.");
                return(new JsonNetResult(jsonModel));
            }

            if (Request.Files.Count > 1)
            {
                jsonModel = JsonModel.CreateFailure("Too many files uploaded.");
                return(new JsonNetResult(jsonModel));
            }

            var file = Request.Files[0];

            if (file == null || file.ContentLength == 0)
            {
                jsonModel = JsonModel.CreateFailure("The file has no content.");
                return(new JsonNetResult(jsonModel));
            }

            const long MAX_FILE_SIZE = 5120000; //5MB

            if (file.ContentLength > MAX_FILE_SIZE)
            {
                jsonModel = JsonModel.CreateFailure(string.Format("Max file size {0} MB", MAX_FILE_SIZE / 1024000));
                return(new JsonNetResult(jsonModel));
            }

            //Set file name for uploaded file and remove file suffix for layer name
            var fileName = file.FileName;

            try
            {
                var jsonString = string.Empty;
                if (file.ContentType == "application/zip" || file.ContentType == "application/x-zip-compressed" ||
                    fileName.ToLower().EndsWith(".zip")) //|| file.ContentType == "application/octet-stream")
                {
                    jsonString = MySettingsManager.GetGeoJsonFromShapeZip(GetCurrentUser(), file.FileName,
                                                                          file.InputStream);

                    //Change file suffix since we converted shape to geojson
                    fileName = fileName.Replace(".zip", ".geojson");
                }
                else
                {
                    jsonString = new StreamReader(file.InputStream).ReadToEnd();
                }

                /*if (MySettingsManager.MapDataFileExists(GetCurrentUser(), fileName))
                 * {
                 *  jsonModel = JsonModel.CreateFailure(Resource.SharedFileAlreadyExists);
                 *  return new JsonNetResult(jsonModel);
                 * }*/

                var featureCollection =
                    JsonConvert.DeserializeObject(jsonString, typeof(FeatureCollection)) as FeatureCollection;

                if (featureCollection == null)
                {
                    jsonModel = JsonModel.CreateFailure(Resource.UploadFileJsonSerialaztionFaild);
                    return(new JsonNetResult(jsonModel));
                }

                CoordinateSystem coordinateSystem = null;
                if (coordinateSystemId.HasValue)
                {
                    coordinateSystem = new CoordinateSystem((CoordinateSystemId)coordinateSystemId.Value);
                }
                else
                {
                    coordinateSystem = GisTools.GeoJsonUtils.FindCoordinateSystem(featureCollection);
                    if (coordinateSystem.Id == CoordinateSystemId.None)
                    {
                        jsonModel = JsonModel.CreateFailure(Resource.FilterSpatialUnableToDetermineCoordinateSystem);
                        return(new JsonNetResult(jsonModel));
                    }
                }

                //Make sure crs is saved in file
                if (featureCollection.CRS == null)
                {
                    featureCollection.CRS = new NamedCRS(coordinateSystem.Id.EpsgCode());
                }

                //create a Json object from string
                var jsonObject = JObject.FromObject(featureCollection);

                //Sava json to file
                using (var fileStream = jsonObject.ToString().ToStream())
                {
                    MySettingsManager.SaveMapDataFile(GetCurrentUser(), fileName, fileStream);
                }

                if (AddFileWfsLayer(fileName, featureCollection))
                {
                    jsonModel = JsonModel.CreateSuccess("Ok");
                }
                else
                {
                    jsonModel = JsonModel.CreateFailure(Resource.UploafFileLayerAlreadyAdded);
                }
            }
            catch (ImportGisFileException importGisFileException)
            {
                jsonModel = JsonModel.CreateFailure(importGisFileException.Message);
            }
            catch (Exception ex)
            {
                jsonModel = JsonModel.CreateFailure(Resource.FilterSpatialUnableToParseGeoJsonFile);
            }

            return(new JsonNetResult(jsonModel));
        }