/// <summary> /// Operation Dynamic Layer /// </summary> /// <param name="boundVariables">bound Variables</param> /// <param name="operationInput">operation Input</param> /// <param name="outputFormat">output Format</param> /// <param name="requestProperties">request Properties</param> /// <param name="responseProperties">response Properties</param> /// <returns>Dynamic Layer</returns> private byte[] DynamicLayerOperationHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; try { JsonObject featureSet; bool found = operationInput.TryGetJsonObject("featureSet", out featureSet); if (!found) { throw new ArgumentNullException(nameof(featureSet)); } if (this.workspace == null) { throw new FactoryUtilitiesException("Workspace non impostato!"); } if (string.IsNullOrWhiteSpace(this.workspaceId)) { throw new FactoryUtilitiesException("WorkspaceId non impostato!"); } IJSONReader jsonReader = new JSONReaderClass(); jsonReader.ReadFromString(featureSet.ToJson()); IJSONConverterGdb JSONConverterGdb = new JSONConverterGdbClass(); IPropertySet originalToNewFieldMap; IRecordSet recorset; JSONConverterGdb.ReadRecordSet(jsonReader, null, null, out recorset, out originalToNewFieldMap); IRecordSet2 recordSet2 = recorset as IRecordSet2; string featureClassName = string.Format("D{0}{1}", DateTime.Now.ToString("yyyyMMdd"), Guid.NewGuid().ToString()).Replace("-", ""); recordSet2.SaveAsTable(this.workspace, featureClassName); JsonObject result = new JsonObject(); result.AddString("name", featureClassName); result.AddString("workspaceId", this.workspaceId); result.AddBoolean("hasError", false); return(result.JsonByte()); } catch (Exception ex) { ObjectError o = new ObjectError(ex.Message); return(o.ToJsonObject().JsonByte()); } }
/// <summary> /// Operation Extract Data /// </summary> /// <param name="boundVariables">bound Variables</param> /// <param name="operationInput">operation Input</param> /// <param name="outputFormat">output Format</param> /// <param name="requestProperties">request Properties</param> /// <param name="responseProperties">response Properties</param> /// <returns>Extract Data</returns> private byte[] ExtractDataOperationHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; try { // input parameters // input data format string dataFormatValue; bool found = operationInput.TryGetString("dataFormat", out dataFormatValue); if (!found || string.IsNullOrEmpty(dataFormatValue)) { throw new ArgumentNullException(nameof(dataFormatValue)); } DataFormat dataFormat; if (!Enum.TryParse <DataFormat>(dataFormatValue, out dataFormat)) { throw new ArgumentNullException(nameof(dataFormatValue)); } // il parametro urlLayer e geometry sono obbligatorio per il KMZ string urlLayerValue = null; JsonObject geometryValue = null; string tokenValue = null; // input featureset JsonObject featureSetValue = null; if (dataFormat == DataFormat.KMZ) { found = operationInput.TryGetString("urlLayer", out urlLayerValue); if (!found || string.IsNullOrEmpty(urlLayerValue)) { throw new ArgumentNullException(nameof(urlLayerValue)); } urlLayerValue = urlLayerValue.TrimEnd('/'); found = operationInput.TryGetString("token", out tokenValue); found = operationInput.TryGetJsonObject("geometry", out geometryValue); if (!found) { throw new ArgumentNullException(nameof(geometryValue)); } } else { found = operationInput.TryGetJsonObject("featureSet", out featureSetValue); if (!found) { throw new ArgumentNullException(nameof(featureSetValue)); } } // input outputName string outputNameValue; found = operationInput.TryGetString("outputName", out outputNameValue); if (!found || string.IsNullOrEmpty(outputNameValue)) { throw new ArgumentNullException(nameof(outputNameValue)); } // lascio solo caratteri e numeri outputNameValue = new String(outputNameValue.Where(Char.IsLetterOrDigit).ToArray()); if (string.IsNullOrEmpty(outputNameValue)) { throw new ArgumentNullException(nameof(outputNameValue)); } // se il nome inizia con un numero metto davanti una lettera if (Char.IsDigit(outputNameValue.FirstOrDefault())) { outputNameValue = $"f{outputNameValue}"; } // se il formato è shapefile il nome del file lo tronco a max 8 caratteri if ((dataFormat == DataFormat.SHAPEFILE) && (outputNameValue.Length > 8)) { outputNameValue = outputNameValue.Substring(0, 8); } // creazione cartella di output string folderNameOutput = Guid.NewGuid().ToString(); string pathOutput = System.IO.Path.Combine(this.pathOutputAGS, folderNameOutput); if (Directory.Exists(pathOutput)) { // non dovrebbe mai verificarsi throw new FactoryUtilitiesException($"Cartella {pathOutput} già esistente"); } else { Directory.CreateDirectory(pathOutput); } // cartella kmz string kmzFolder = null; // cartella shapefile string shapefileFolder = null; // cartella csv string csvFolder = null; // cartella kml string kmlFolder = null; // file geodatabase string fGDBFolder = null; JsonObject message = null; if (dataFormat == DataFormat.KMZ) { kmzFolder = System.IO.Path.Combine(pathOutput, "kmz"); Directory.CreateDirectory(kmzFolder); this.ConvertFeatureClassPointToKmz(urlLayerValue, geometryValue, tokenValue, outputNameValue, kmzFolder); } else { List <string> errorFieldShapeFile = null; List <string> invalidObjectIDShapeFile = null; IJSONReader jsonReader = new JSONReaderClass(); jsonReader.ReadFromString(featureSetValue.ToJson()); IJSONConverterGdb JSONConverterGdb = new JSONConverterGdbClass(); IPropertySet originalToNewFieldMap; IRecordSet recorset; JSONConverterGdb.ReadRecordSet(jsonReader, null, null, out recorset, out originalToNewFieldMap); IRecordSet2 recordSet2 = recorset as IRecordSet2; ITable t = null; IWorkspace workspaceGDB = null; try { // nome del file geodatabase con estensione string nameGDB = System.IO.Path.ChangeExtension(outputNameValue, Enum.GetName(typeof(FileExtension), FileExtension.gdb)); // creazione del file geodatabase workspaceGDB = Helper.CreateFileGdbWorkspace(pathOutput, nameGDB); t = recordSet2.SaveAsTable(workspaceGDB, outputNameValue); if (dataFormat == DataFormat.SHAPEFILE) { shapefileFolder = System.IO.Path.Combine(pathOutput, Enum.GetName(typeof(DataFormat), DataFormat.SHAPEFILE).ToLowerInvariant()); errorFieldShapeFile = new List <string>(); invalidObjectIDShapeFile = new List <string>(); Directory.CreateDirectory(shapefileFolder); this.ConvertFeatureClassToShapefile(workspaceGDB, outputNameValue, shapefileFolder, ref errorFieldShapeFile, ref invalidObjectIDShapeFile); if ((errorFieldShapeFile.Count > 0) || (invalidObjectIDShapeFile.Count > 0)) { message = new JsonObject(); message.AddArray("errorField", errorFieldShapeFile.ToArray()); message.AddArray("invalidObjectID", invalidObjectIDShapeFile.ToArray()); } } else if (dataFormat == DataFormat.CSV) { csvFolder = System.IO.Path.Combine(pathOutput, Enum.GetName(typeof(DataFormat), DataFormat.CSV).ToLowerInvariant()); Directory.CreateDirectory(csvFolder); this.ConvertFeatureClassPointToCsv(workspaceGDB, outputNameValue, csvFolder); } else if (dataFormat == DataFormat.KML) { // funzione per creare il file kml con la libreria SharpKml // file è senza render kmlFolder = System.IO.Path.Combine(pathOutput, Enum.GetName(typeof(DataFormat), DataFormat.KML).ToLowerInvariant()); Directory.CreateDirectory(kmlFolder); this.ConvertFeatureClassPointToKml(workspaceGDB, outputNameValue, kmlFolder); } else if (dataFormat == DataFormat.FILEGEODATABASE) { fGDBFolder = System.IO.Path.Combine(pathOutput, nameGDB); } } catch { throw; } finally { Marshal.FinalReleaseComObject(t); Marshal.FinalReleaseComObject(workspaceGDB); } } // nome del file zip con estensione string nameZip = System.IO.Path.ChangeExtension(outputNameValue, Enum.GetName(typeof(FileExtension), FileExtension.zip)); // percorso e nome file zip con estensione string pathFileOutputZip = System.IO.Path.Combine(pathOutput, nameZip); if (dataFormat == DataFormat.FILEGEODATABASE) { ZipFile.CreateFromDirectory(fGDBFolder, pathFileOutputZip, CompressionLevel.Fastest, true); } else if (dataFormat == DataFormat.SHAPEFILE) { ZipFile.CreateFromDirectory(shapefileFolder, pathFileOutputZip, CompressionLevel.Fastest, true); } else if (dataFormat == DataFormat.CSV) { ZipFile.CreateFromDirectory(csvFolder, pathFileOutputZip, CompressionLevel.Fastest, true); } else if (dataFormat == DataFormat.KML) { ZipFile.CreateFromDirectory(kmlFolder, pathFileOutputZip, CompressionLevel.Fastest, true); } else if (dataFormat == DataFormat.KMZ) { ZipFile.CreateFromDirectory(kmzFolder, pathFileOutputZip, CompressionLevel.Fastest, true); } JsonObject result = new JsonObject(); result.AddString("url", Helper.CombineUri(this.pathOutputVirtualAGS, Helper.CombineUri(folderNameOutput, nameZip))); result.AddBoolean("hasError", false); if (message != null) { result.AddJsonObject("extraInfo", message); } return(result.JsonByte()); } catch (Exception ex) { ObjectError o = new ObjectError(ex.Message); return(o.ToJsonObject().JsonByte()); } }