private byte[] ExportGeoJsonHandler(NameValueCollection boundVariables,
                                            JsonObject operationInput,
                                            string outputFormat,
                                            string requestProperties,
                                            out string responseProperties)
        {
            responseProperties = "{\"Content-Type\" : \"application/json\"}";;
            bool     applyQuery = true;
            bool     useBbox    = true;
            bool     useBboxSR  = true;
            string   retval     = "";
            string   whereClause;
            string   boxClause;
            int      bboxSRID  = 0;
            IPolygon queryGeom = null;
            Helpers  helper    = new Helpers();
            bool     found     = operationInput.TryGetString("query", out whereClause);

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

            string ssrid;

            found = operationInput.TryGetString("bboxSR", out ssrid);
            if (!found || string.IsNullOrEmpty(ssrid))
            {
                //then no definition query
                useBboxSR = false;
            }
            else
            {
                int  srid;
                bool valid = int.TryParse(ssrid, out srid);
                if (valid)
                {
                    bboxSRID = srid;
                }
                else
                {
                    useBboxSR = false;
                }
            }

            found = operationInput.TryGetString("bbox", out boxClause);
            if (!found || string.IsNullOrEmpty(boxClause))
            {
                //then no definition query
                useBbox = false;
            }

            if (useBbox)
            {
                try
                {
                    double   xmin; double ymin; double xmax; double ymax;
                    string[] vals = boxClause.Split(new char[] { ',' });
                    if (vals.Length == 4)
                    {
                        bool bxmin = double.TryParse(vals[0], out xmin);
                        bool bymin = double.TryParse(vals[1], out ymin);
                        bool bxmax = double.TryParse(vals[2], out xmax);
                        bool bymax = double.TryParse(vals[3], out ymax);

                        if (bxmin && bymin && bxmax && bymax)
                        {
                            ISpatialReference sr = null;
                            if (useBboxSR)
                            {
                                sr = helper.GetSpatialReference(bboxSRID);
                                if (sr == null)
                                {
                                    //erroneous srid, ignore bounding box
                                    useBbox = false;
                                }
                            }
                            else
                            {
                                sr = helper.getWGS84();
                            }
                            if (useBbox)
                            {
                                queryGeom = new Polygon() as IPolygon;
                                IPointCollection coll = queryGeom as IPointCollection;
                                coll.AddPoint(new Point()
                                {
                                    X = xmin, Y = ymin, SpatialReference = sr
                                });
                                coll.AddPoint(new Point()
                                {
                                    X = xmin, Y = ymax, SpatialReference = sr
                                });
                                coll.AddPoint(new Point()
                                {
                                    X = xmax, Y = ymax, SpatialReference = sr
                                });
                                coll.AddPoint(new Point()
                                {
                                    X = xmax, Y = ymin, SpatialReference = sr
                                });
                                coll.AddPoint(new Point()
                                {
                                    X = xmin, Y = ymin, SpatialReference = sr
                                });
                                queryGeom.SpatialReference = sr;
                            }
                        }
                        else
                        {
                            useBbox = false;
                        }
                    }
                    else
                    {
                        useBbox = false;
                    }
                }
                catch
                {
                    useBbox = false;
                }
            }

            long?layerOrdinal;

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

            string s = "";

            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;
                retval = "{\"shape\": \"" + fclass.ShapeFieldName + "\"}";
                IQueryFilter filter = null;
                if (useBbox)
                {
                    IGeoDataset gds = fclass as IGeoDataset;
                    filter = new SpatialFilterClass();
                    ISpatialFilter spf = filter as ISpatialFilter;
                    spf.Geometry      = helper.TransformShapeCS(queryGeom, queryGeom.SpatialReference, gds.SpatialReference);
                    spf.GeometryField = fclass.ShapeFieldName;
                    spf.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;
                }
                else
                {
                    filter = new QueryFilterClass();
                }
                filter.set_OutputSpatialReference(fclass.ShapeFieldName, getWGS84());
                if (applyQuery)
                {
                    filter.WhereClause = whereClause;
                }

                IFeatureCursor curs = fclass.Search(filter, false);
                //apply extension methods here
                try
                {
                    s = curs.ToGeoJson();
                    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(retval));
        }
Beispiel #2
0
        private byte[] ExportGeoJsonHandler(NameValueCollection boundVariables,
                                            JsonObject operationInput,
                                            string outputFormat,
                                            string requestProperties,
                                            out string responseProperties)
        {
            responseProperties = null;
            bool   applyQuery = true;
            string retval     = "";
            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");
            }

            string s = "";

            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;
                retval = "{\"shape\": \"" + fclass.ShapeFieldName + "\"}";
                IQueryFilter filter = new QueryFilterClass();
                filter.set_OutputSpatialReference(fclass.ShapeFieldName, getWGS84());
                if (applyQuery)
                {
                    filter.WhereClause = whereClause;
                }
                IFeatureCursor curs = fclass.Search(filter, false);
                //apply extension methods here
                try
                {
                    s = curs.ToGeoJson();
                    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(retval));
        }
        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? applyGeoms = true;
            bool addHeader = false;

            //bool addGeoms = 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;
                }
            }

            //bool useGeoms = operationInput.TryGetAsBoolean("addgeoms", out applyGeoms);
            //if (useGeoms)
            //{
            //    if ((bool)applyGeoms)
            //    {
            //        addGeoms = 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;
                sb.Append(retval);
            }
            else
            {
                throw new Exception("Layer " + layerOrdinal.ToString() + " is not a feature layer.");
            }

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