private byte[] ExportLayerHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat,
            string requestProperties, out string responseProperties)
        {
            responseProperties = null;

            try
            {

                //hydrate the input
                JsonObject feedPropertiesObject = null;
                JsonObject jsonLocation;
                IGeometry location = null;
                IExportProperties exportProperties = null;
                string whereClause;
                string geometryType;
                GPX.Server.Extension.Spatial.MapServer mapserver = new Spatial.MapServer(serverObjectHelper.ServerObject);

                logger.LogMessage(ServerLogger.msgType.infoDetailed, "ExportLayerHandler", 999999, "Beginning Export");

                //layerID
                int layerID = Convert.ToInt32(boundVariables["ExportLayersId"]);

                if (!operationInput.TryGetJsonObject("exportProperties", out feedPropertiesObject))
                    throw new ArgumentException("Error: Could not parse exportProperties" + feedPropertiesObject.ToJson());

                //initialise the correct export properties
                if (feedPropertiesObject != null)
                {
                    if (outputFormat == GEORSS_FORMAT)
                    {

                        exportProperties = new GeoRSSExportProperties(feedPropertiesObject.ToJson());

                    }
                    else
                    {
                        exportProperties = new GeoJsonExportProperties(feedPropertiesObject.ToJson());
                    }
                }

                if (!operationInput.TryGetJsonObject("filterGeometry", out jsonLocation))
                    throw new ArgumentNullException("filterGeometry");

                if (jsonLocation != null)
                {
                    if (!operationInput.TryGetString("geometryType", out geometryType))
                        throw new ArgumentNullException("Can supply a geometry without a geometryType");

                    switch (geometryType)
                    {
                        case "Polygon":
                            location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPolygon);
                            if (location == null)
                                throw new ArgumentException("ExportLayerHandler: invalid polygon", "filterGeometry");
                            break;
                        case "Point":
                            location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPoint);
                            if (location == null)
                                throw new ArgumentException("ExportLayerHandler: invalid point", "filterGeometry");
                            break;
                        case "Line":
                            location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPolyline);
                            if (location == null)
                                throw new ArgumentException("ExportLayerHandler: invalid polyline", "filterGeometry");
                            break;
                        default:
                            break;
                    }

                }

                if (!operationInput.TryGetString("where", out whereClause))
                    throw new ArgumentNullException("where");

                //run the query on the map server
                RecordSet results = mapserver.Query(layerID, location, whereClause, exportProperties.GeometryField, exportProperties.OutputSpatialReference);

                //generate the export
                string finalExport = string.Empty;
                string xmlString = string.Empty;

                if (outputFormat == GEORSS_FORMAT)
                {
                    if (exportProperties != null)
                    {

                        var feedProperties = (GeoRSSExportProperties)exportProperties;
                        GeoRSSExport export = new GeoRSSExport();
                        export.CreateExport(results, exportProperties);

                        StringBuilder sb = new StringBuilder();
                        XmlWriter xmlWriter = XmlWriter.Create(sb);

                        if (feedProperties.FeedFormat == "Atom")
                        {
                            export.SaveAsAtom10(xmlWriter);
                            responseProperties = "{\"Content-Type\" : \"application/atom+xml\"}";
                        }
                        else
                        {
                            export.SaveAsRss20(xmlWriter);
                            responseProperties = "{\"Content-Type\" : \"application/rss+xml\"}";
                        }

                        xmlWriter.Close();
                        xmlString = sb.ToString();

                        //todo  - is it the xmlwriter that applies the encoding - setting the xmlwritersettings.encoding does not help!
                        finalExport = xmlString.Replace("utf-16", "utf-8");

                    }
                }
                else
                {
                    //set response properties
                    responseProperties = null;

                    //if the export properties passed is not null then create the export
                    if (feedPropertiesObject != null)
                    {

                        EsriToGeoJson.GeoJsonExport export = new EsriToGeoJson.GeoJsonExport();
                        export.CreateExport(results, exportProperties.GeometryField);
                        finalExport = export.GeoJson;

                    }

                }

                return Encoding.UTF8.GetBytes(finalExport);
            }
            catch (Exception ex)
            {
                logger.LogMessage(ServerLogger.msgType.error, "ExportLayerHandler", 999999, ex.ToString());
                responseProperties = null;
                string error = JsonConvert.SerializeObject(ex, Newtonsoft.Json.Formatting.Indented);
                return Encoding.UTF8.GetBytes(error);

            }
            finally
            {

            }
        }
        private List<SyndicationItem> CreateItemCollection(RecordSet results, GeoRSSExportProperties feedProperties)
        {
            List<SyndicationItem> items = new List<SyndicationItem>();
            ICursor cursor;
            SimpleGeometryFactory simpleFactory = new SimpleGeometryFactory();
            GMLGeometryFactory gmlFactory = new GMLGeometryFactory();

            try
            {
                //create a feed item for each record
                cursor = results.get_Cursor(false);

                Exporter.logger.LogMessage(ESRI.ArcGIS.SOESupport.ServerLogger.msgType.infoStandard, "CreateItemCollection", 999999, "Cursor Set");
                IRow row = cursor.NextRow();

                int rowCount = 0;

                if (row != null)
                {
                    while (row != null)
                    {
                        //on the first row log out the field name and field alias
                        if (rowCount == 0)
                        {
                            for (int r = 0; r < row.Fields.FieldCount; r++)
                            {
                                IField field = row.Fields.get_Field(r);

                                Exporter.logger.LogMessage(ESRI.ArcGIS.SOESupport.ServerLogger.msgType.infoDetailed,
                                "CreateItemCollection",
                                999999,
                                "field Name: " + field.Name +
                                " field alias: " + field.AliasName
                                );
                            }
                        }

                        SyndicationItem item = new SyndicationItem();

                        //set the item properties
                        foreach (KeyValuePair<string, GeoRSSExportItem> itemConfig in feedProperties.Items)
                        {
                            GeoRSSExportItem itemExportProperty = itemConfig.Value;

                            if (itemExportProperty != null)
                            {
                                SetItemValues(row, ref item, itemConfig, itemExportProperty);

                            }

                        }

                        //process the geometry

                        int fieldIndex = row.Fields.FindField(feedProperties.GeometryField);

                        if (fieldIndex == -1)
                            throw new Exception("Could not locate geometry field:" + feedProperties.GeometryField);

                        if (row.get_Value(fieldIndex) != null)
                        {
                            IGeometry geom = row.get_Value(fieldIndex) as IGeometry;
                            GeoRSSGeometry geometry = null;
                            switch (feedProperties.GetGeoRSSGeomtryType())
                            {
                                case GeoRSSGeometryFormat.Simple:
                                    geometry = simpleFactory.GetGeometry(geom);
                                    break;
                                case GeoRSSGeometryFormat.GML:
                                    geometry = gmlFactory.GetGeometry(geom);
                                    break;
                                default:
                                    break;
                            }

                            if (geom != null)
                                item.ElementExtensions.Add(geometry.GeometryAsXML);
                        }

                        items.Add(item);

                        row = cursor.NextRow();

                        rowCount++;

                    }
                }
                return items;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cursor = null;
            }
        }