Esempio n. 1
0
        private byte[] ExportCsvHandler(NameValueCollection boundVariables,
                                        JsonObject operationInput,
                                        string outputFormat,
                                        string requestProperties,
                                        out string responseProperties)
        {
            string        retval      = "";
            StringBuilder sb          = new StringBuilder();
            string        s           = "";
            bool          applyQuery  = true;
            bool?         applyHeader = true;
            bool          addHeader   = false;

            responseProperties = "{\"Content-Type\" : \"text/csv\"}";

            string whereClause = "";
            bool   found       = operationInput.TryGetString("query", out whereClause);

            if (!found || string.IsNullOrEmpty(whereClause))
            {
                //then no definition query
                applyQuery = false;
            }

            long?layerOrdinal;

            found = operationInput.TryGetAsLong("layer", out layerOrdinal); //.TryGetString("layer", out parm2Value);
            if (!found)
            {
                throw new ArgumentNullException("layer");
            }

            bool useHeader = operationInput.TryGetAsBoolean("headers", out applyHeader);

            if (useHeader)
            {
                if ((bool)applyHeader)
                {
                    addHeader = true;
                }
            }

            ESRI.ArcGIS.Carto.IMapServer           mapServer        = (ESRI.ArcGIS.Carto.IMapServer)serverObjectHelper.ServerObject;
            ESRI.ArcGIS.Carto.IMapServerDataAccess mapServerObjects = (ESRI.ArcGIS.Carto.IMapServerDataAccess)mapServer;
            var lyr = mapServerObjects.GetDataSource(mapServer.DefaultMapName, Convert.ToInt32(layerOrdinal));

            if (lyr is IFeatureClass)
            {
                IFeatureClass fclass = (IFeatureClass)lyr;
                IQueryFilter  filter = new QueryFilterClass();
                filter.set_OutputSpatialReference(fclass.ShapeFieldName, getWGS84());
                if (applyQuery)
                {
                    filter.WhereClause = whereClause;
                }
                IFeatureCursor curs = fclass.Search(filter, false);
                try
                {
                    //();
                    s = curs.ToCSV(addHeader);
                    Marshal.ReleaseComObject(curs);
                }
                catch (Exception ex)
                {
                    s = ex.GetBaseException().ToString(); //.StackTrace;
                }
                retval = s;
            }
            else
            {
                throw new Exception("Layer " + layerOrdinal.ToString() + " is not a feature layer.");
            }

            return(Encoding.UTF8.GetBytes(sb.ToString()));
        }