public void RoadSegmentsToGeoJSONTest()
        {
            string connectionString      = TestGeospatialServices.Properties.Settings.Default.TestDb;
            string providerInvariantName = "System.Data.SqlClient";
            string sql      = "SELECT * FROM road_segments";
            string json     = "{}";
            string expected = "{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"LineString\", \"coordinates\":[[0, 18], [10, 21], [16, 23], [28, 26], [44, 31]]}, \"properties\":{\"fid\":102, \"name\":\"Route 5\", \"aliases\":null, \"num_lanes\":2}}, {\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"LineString\", \"coordinates\":[[44, 31], [56, 34], [70, 38]]}, \"properties\":{\"fid\":103, \"name\":\"Route 5\", \"aliases\":\"Main Street\", \"num_lanes\":4}}, {\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"LineString\", \"coordinates\":[[70, 38], [72, 48]]}, \"properties\":{\"fid\":104, \"name\":\"Route 5\", \"aliases\":null, \"num_lanes\":2}}, {\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"LineString\", \"coordinates\":[[70, 38], [84, 42]]}, \"properties\":{\"fid\":105, \"name\":\"Main Street\", \"aliases\":null, \"num_lanes\":4}}, {\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"LineString\", \"coordinates\":[[28, 26], [28, 0]]}, \"properties\":{\"fid\":106, \"name\":\"Dirt Road by Green Forest\", \"aliases\":null, \"num_lanes\":1}}]}";
            string actual;

            actual = GeoJSONService.DatabaseRequest(json, connectionString, providerInvariantName, sql);
            Assert.AreEqual(expected, actual);
        }
        public void GeoJSONServiceWithGeometryTest()
        {
            string connectionString      = TestGeospatialServices.Properties.Settings.Default.TestDb;
            string providerInvariantName = "System.Data.SqlClient";
            string sql      = "SELECT count(*) as city_count, SUM(POP_1990) total_pop_1990, SUM(cast(POPULATION as float)) total_pop_2009 FROM us_cities where geom.STIntersects(Geometry::STGeomFromText(@wkt,@srid)) = 1 and WATCH_WARN = @WATCH_WARN";
            string json     = "{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[-88, 40], [-84, 40], [-84, 43], [-88, 40], [-88, 40]]]}, \"properties\":{\"WATCH_WARN\": 1}, \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:4326\"}}}]}";
            string expected = "{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"geometry\":null, \"properties\":{\"city_count\":71, \"total_pop_1990\":1064299, \"total_pop_2009\":1139609}}]}";
            string actual;

            actual = GeoJSONService.DatabaseRequest(json, connectionString, providerInvariantName, sql);
            Assert.AreEqual(expected, actual);
        }
        public void LakesToGeoJSONTest()
        {
            string connectionString      = TestGeospatialServices.Properties.Settings.Default.TestDb;
            string providerInvariantName = "System.Data.SqlClient";
            string sql      = "SELECT * FROM lakes";
            string json     = "{}";
            string expected = "{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:101\"}}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[52, 18], [66, 23], [73, 9], [48, 6], [52, 18], [59, 18], [67, 18], [67, 13], [59, 13], [59, 18]], [[52, 18], [66, 23], [73, 9], [48, 6], [52, 18], [59, 18], [67, 18], [67, 13], [59, 13], [59, 18]]]}, \"properties\":{\"fid\":101, \"name\":\"BLUE LAKE\"}}]}";
            string actual;

            actual = GeoJSONService.DatabaseRequest(json, connectionString, providerInvariantName, sql);
            Assert.AreEqual(expected, actual);
        }
        public void SqlToGeoJSONTest()
        {
            string connectionString      = TestGeospatialServices.Properties.Settings.Default.TestDb;
            string providerInvariantName = "System.Data.SqlClient";
            string sql      = "SELECT * FROM us_cities where FID = 1";
            string json     = "{}";
            string expected = "{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"epsg:4326\"}}, \"geometry\":{\"type\":\"Point\", \"coordinates\":[-81.6717, 31.7528]}, \"properties\":{\"FID\":1, \"ST_FIPS\":\"13\", \"SFIPS\":\"13\", \"COUNTY_FIP\":\"179\", \"CFIPS\":\"179\", \"PL_FIPS\":\"80256\", \"ID\":\"348148\", \"ELEVATION\":\"71\", \"POP_1990\":2024, \"POPULATION\":\"4030\", \"NAME\":\"AIMAR\", \"ST\":\"GA\", \"STATE\":\"GEORGIA\", \"WARNGENLEV\":\"3\", \"WARNGENTYP\":\"\", \"WATCH_WARN\":\"3\", \"ZWATCH_WAR\":0, \"PROG_DISC\":230, \"ZPROG_DISC\":3640, \"COMBOFLAG\":0, \"LAND_WATER\":\"\", \"RECNUM\":0, \"LON\":-81.6717, \"LAT\":31.7528}}]}";
            string actual;

            actual = GeoJSONService.DatabaseRequest(json, connectionString, providerInvariantName, sql);
            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        public void ProcessRequest(HttpContext context)
        {
            // parse and decode http request
            System.IO.Stream       body     = context.Request.InputStream;
            System.Text.Encoding   encoding = context.Request.ContentEncoding;
            System.IO.StreamReader reader   = new System.IO.StreamReader(body, encoding);
            string s = reader.ReadToEnd();

            s = HttpUtility.UrlDecode(s);
            body.Close();
            reader.Close();

            //if (!System.Text.RegularExpressions.Regex.IsMatch(
            //        context.Request.QueryString["Name"], @"^[a-zA-Z'.\s]{1,40}$"))
            //    throw new ArgumentException("Invalid request parameter");

            //get geoprocessing method
            string geoMethod  = context.Request["method"];
            string geoDataset = context.Request["dataset"];

            string json = null;

            if (geoMethod == "spatialquery")
            {
                string connection = global::System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                string provider   = global::System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ProviderName;
                string sql        = ConfigurationSettings.AppSettings[geoDataset];
                json = GeoJSONService.DatabaseRequest(s, connection, provider, sql);
            }

            if (geoMethod == "buffer")
            {
                json = GeoJSONService.GeoprocessingRequest(geoMethod, s);
            }

            // create response

            //Ajax Web Service request always starts with application/json to help prevent XSS attacks see Scott Gutheries blog on this
            if (context.Request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json"))
            {
                //User may be using an older version of IE which does not support compression, so skip those
                if (!((context.Request.Browser.IsBrowser("IE")) && (context.Request.Browser.MajorVersion <= 6)))
                {
                    string acceptEncoding = context.Request.Headers["Accept-Encoding"];

                    if (!string.IsNullOrEmpty(acceptEncoding))
                    {
                        acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);

                        if (acceptEncoding.Contains("gzip"))
                        {
                            context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                            context.Response.AddHeader("Content-encoding", "gzip");
                        }
                        else if (acceptEncoding.Contains("deflate"))
                        {
                            context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                            context.Response.AddHeader("Content-encoding", "deflate");
                        }
                    }
                }
                context.Response.Write(json);
            }
        }