Esempio n. 1
0
        public ActionResult AddWfsLayer(string url, WFSVersion wfsVersion)
        {
            string wfsUrl = null;

            try
            {
                wfsUrl = WFSManager.CreateGetCapabiltiesRequestUrl(url, wfsVersion);
            }
            catch (UriFormatException)
            {
                ModelState.AddModelError("", "The URL has wrong format");
            }

            if (ModelState.IsValid)
            {
                if (wfsVersion == WFSVersion.Unknown)
                {
                    wfsVersion = WFSManager.GetWFSVersionFromRequestUrl(url);
                    if (wfsVersion == WFSVersion.Unknown)
                    {
                        wfsVersion = WFSVersion.Ver110;
                    }
                }
                return(RedirectToAction("AddWfsLayer", new { @url = wfsUrl }));
            }
            var viewManager = new WfsLayersViewManager(GetCurrentUser(), SessionHandler.MySettings);
            var model       = viewManager.CreateAddWfsLayerViewModel(ModelState, url, Url.Action("UploadMapDataFile"), Url.Action("WfsLayers"));

            return(View(model));
        }
Esempio n. 2
0
        /// <summary>
        /// Requests the WFS capabilities from a web server.
        /// </summary>
        /// <param name="url">The WFS URL.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        /// <remarks>The function version parameter is chosen before the query string version parameter</remarks>
        public static WFSCapabilities GetWFSCapabilities(string url, WFSVersion version)
        {
            string requestUrl = CreateGetCapabiltiesRequestUrl(url, version);

            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                string strXml = wc.DownloadString(requestUrl);
                return(ParseCapabilities(strXml, version));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Parses a WFS capabilities XML string.
        /// </summary>
        /// <param name="strXml">The WFS xml string.</param>
        /// <param name="version">The version.</param>
        /// <returns></returns>
        public static WFSCapabilities ParseCapabilities(string strXml, WFSVersion version)
        {
            switch (version)
            {
            case WFSVersion.Ver100:
                return(WFSCapabilitiesParserVer100.Parse(strXml));

            case WFSVersion.Ver110:
                return(WFSCapabilitiesParserVer110.Parse(strXml));

            default:
                throw new Exception("WFS Version is unknown");
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a WFS GetCapabilities url.
        /// </summary>
        /// <param name="strUrl">The url to the wfs service</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static string CreateGetCapabiltiesRequestUrl(string strUrl, WFSVersion version)
        {
            if (version == WFSVersion.Unknown)
            {
                throw new Exception("WFSVersion is unknown");
            }

            var strVersion = GetWFSVersionStringFromEnum(version);
            var uriBuilder = new UriBuilder(strUrl);
            var uri        = uriBuilder.Uri;
            var baseUrl    = uri.GetLeftPart(UriPartial.Path);

            return(string.Format("{0}?service=wfs&request=GetCapabilities&version={1}", baseUrl, strVersion));
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the WFS version string from WFSVersion enum.
        /// </summary>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static string GetWFSVersionStringFromEnum(WFSVersion version)
        {
            switch (version)
            {
            case WFSVersion.Ver100:
                return("1.0.0");

            case WFSVersion.Ver110:
                return("1.1.0");

            default:
                throw new Exception("WFS Version is unknown");
            }
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="serverUrl">The WFS server URL.</param>
        /// <param name="version">The WFS version.</param>
        /// <param name="bbox">The bounding box in which the analysyis should be done.</param>
        /// <param name="typeName">The features layer names you want info about.</param>
        /// <param name="srsName">The spatial reference system (SRID) in the format of EPSG:XXXX </param>
        /// <param name="parameter"></param>
        /// <param name="parameterValue"></param>
        /// <returns></returns>
        public static FeatureCollection GetWfsFeatures(string serverUrl, WFSVersion version, string bbox, WfsTypeName typeName, string srsName, string parameter, string parameterValue)
        {
            string requestUrl = "";

            requestUrl = CreateGetFeatureRequestUrl(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);

            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                string strJson = wc.DownloadString(requestUrl);

                // Make a Json string to a featue collection type
                FeatureCollection featureCollection = JsonConvert.DeserializeObject(strJson, typeof(FeatureCollection)) as FeatureCollection;
                return(featureCollection);
            }
        }
Esempio n. 7
0
        public void GetWfsFeaturesWithBoundingBox()
        {
            WfsTypeName typeName       = new WfsTypeName();
            WFSVersion  version        = WFSVersion.Ver110;
            string      serverUrl      = "http://slwgeo.artdata.slu.se:8080/geoserver/wfs";
            string      parameter      = string.Empty;
            string      parameterValue = string.Empty;
            string      bbox           = "6400000,1400000, 6500000,1500000";

            typeName.Namespace = "SLW:Sverigekarta_med_lan";
            string            srsName = string.Empty;
            FeatureCollection featureCollection;

            featureCollection = WFSManager.GetWfsFeatures(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);
            Assert.IsNotNull(featureCollection);
            Assert.IsTrue(featureCollection.Features.Count == 5);
        }
Esempio n. 8
0
        public void GetWfsFeatures()
        {
            WfsTypeName typeName       = new WfsTypeName();
            WFSVersion  version        = WFSVersion.Ver110;
            string      serverUrl      = "http://slwgeo.artdata.slu.se:8080/geoserver/SLW/wfs";
            string      parameter      = string.Empty;
            string      parameterValue = string.Empty;
            string      bbox           = string.Empty;//&BBOX=133499, 628499, 154501, 635501;//&BBOX=628499,133499,635501,154501";

            typeName.Namespace = "SLW:Sverigekarta_med_lan";
            string            srsName = string.Empty;
            FeatureCollection featureCollection;

            featureCollection = WFSManager.GetWfsFeatures(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);
            Assert.IsNotNull(featureCollection);
            Assert.IsTrue(featureCollection.Features.Count == 22);
        }
Esempio n. 9
0
        public void GetWfsFeaturesWithFilter()
        {
            WfsTypeName typeName       = new WfsTypeName();
            WFSVersion  version        = WFSVersion.Ver110;
            string      serverUrl      = "http://slwgeo.artdata.slu.se:8080/geoserver/wfs";
            string      bbox           = string.Empty;
            string      parameter      = "SLW:LänSKOD";
            string      parameterValue = "17";

            //Todo: vilken är det:?
            typeName.Namespace = "SLW:Sverigekarta_med_lan";
            //typeName.Name = "SLW:Sverigekarta_med_lan";
            string            srsName = string.Empty;
            FeatureCollection featureCollection;

            featureCollection = WFSManager.GetWfsFeatures(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);
            Assert.IsNotNull(featureCollection);
            Assert.IsTrue(featureCollection.Features.Count == 1);
        }
Esempio n. 10
0
        /// <summary>
        /// Creates a WFS DescribeFeatureType request URL.
        /// </summary>
        /// <param name="requestUrl">The complete url including all parameters except version.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static FeatureCollection GetWfsFeatures(string requestUrl, WFSVersion version)
        {
            const string outputFormat = "json";
            var          strReq       = new StringBuilder();

            strReq.Append(requestUrl);
            strReq.AppendFormat("&outputFormat=" + outputFormat);
            string str = strReq.ToString();

            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                string strJson = wc.DownloadString(str);

                // Make a Json string to a featue collection type
                FeatureCollection featureCollection = JsonConvert.DeserializeObject(strJson, typeof(FeatureCollection)) as FeatureCollection;
                return(featureCollection);
            }
        }
Esempio n. 11
0
 public void GetWfsFeaturesWithBoundingBoxUsingMsFakes()
 {
     using (ShimsContext.Create())
     {
         ShimWebClient.AllInstances.DownloadStringString = (client, url) =>
                                                           File.ReadAllText(@"Sample files\SLW_Sverigekarta_med_lan - BBox Filter.json");
         WfsTypeName typeName       = new WfsTypeName();
         WFSVersion  version        = WFSVersion.Ver110;
         string      serverUrl      = "http://slwgeo.artdata.slu.se:8080/geoserver/wfs";
         string      parameter      = string.Empty;
         string      parameterValue = string.Empty;
         string      bbox           = "6400000,1400000, 6500000,1500000";
         typeName.Namespace = "SLW:Sverigekarta_med_lan";
         string            srsName = string.Empty;
         FeatureCollection featureCollection;
         featureCollection = WFSManager.GetWfsFeatures(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);
         Assert.IsNotNull(featureCollection);
         Assert.AreEqual(5, featureCollection.Features.Count);
     }
 }
Esempio n. 12
0
        public void GetWfsFeaturesUsingMsFakes()
        {
            using (ShimsContext.Create())
            {
                ShimWebClient.AllInstances.DownloadStringString = (client, url) =>
                                                                  File.ReadAllText(@"Sample files\SLW_Sverigekarta_med_lan - All Features.json");

                WfsTypeName typeName       = new WfsTypeName();
                WFSVersion  version        = WFSVersion.Ver110;
                string      serverUrl      = "http://slwgeo.artdata.slu.se:8080/geoserver/SLW/wfs";
                string      parameter      = string.Empty;
                string      parameterValue = string.Empty;
                string      bbox           = string.Empty; // &BBOX=133499, 628499, 154501, 635501;//&BBOX=628499,133499,635501,154501";
                typeName.Namespace = "SLW:Sverigekarta_med_lan";
                string            srsName = string.Empty;
                FeatureCollection featureCollection;
                featureCollection = WFSManager.GetWfsFeatures(serverUrl, version, bbox, typeName, srsName, parameter, parameterValue);
                Assert.IsNotNull(featureCollection);
                Assert.IsTrue(featureCollection.Features.Count == 22);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Creates a WFS DescribeFeatureType request URL.
        /// </summary>
        /// <param name="serverUrl">The WFS server URL.</param>
        /// <param name="version">The WFS version.</param>
        /// <param name="bbox">The bounding box given as bottom left and top right corner coordinates.</param>
        /// <param name="typeName">Name of the feature type to describe.</param>
        /// <param name="srsName">The spatial reference system (SRID) in the format of EPSG:XXXX </param>
        /// <param name="parameter"></param>
        /// <param name="parameterValue"></param>
        /// <returns></returns>
        public static string CreateGetFeatureRequestUrl(string serverUrl, WFSVersion version, string bbox, WfsTypeName typeName, string srsName, string parameter, string parameterValue)
        {
            // As for now only polygon features and output format json are supported
            //
            const string outputFormat = "json";
            string       strFilter    = "";

            if (version == WFSVersion.Unknown)
            {
                throw new Exception("WFSVersion is unknown");
            }

            //The query should specify either typeName, featureId filter(, or a stored query id)
            //
            if (typeName.Namespace.IsEmpty() && parameter.IsEmpty())
            {
                throw new Exception("Missing TypeName and filter in url, wich is not allowed.");
            }

            //The query should not specify both bounding box and filter at the same time
            //
            if (parameter.IsNotEmpty() && bbox.IsNotEmpty())
            {
                throw new Exception("There is a filter and a bounding box defined at the same time: filter and bbox both specified but are mutually exclusive");
                //Todo: Or make the filter with the bounding box
            }

            string strVersion = GetWFSVersionStringFromEnum(version);
            var    uriBuilder = new UriBuilder(serverUrl);
            Uri    uri        = uriBuilder.Uri;
            string baseUrl    = uri.GetLeftPart(UriPartial.Path);
            var    strReq     = new StringBuilder(baseUrl);

            strReq.Append("?");
            strReq.AppendFormat("&service=wfs");
            strReq.AppendFormat("&request=GetFeature");
            strReq.AppendFormat("&version=" + strVersion);

            if (parameter.IsNotEmpty())
            {
                strFilter = CreateWFSFilter(parameter, parameterValue);
                strReq.AppendFormat("&filter=" + strFilter);
            }
            if (bbox.IsNotEmpty())
            {
                strReq.AppendFormat("&bbox=" + bbox);
            }
            if (typeName.Namespace.IsNotEmpty())
            {
                strReq.AppendFormat("&typeName=" + typeName.Namespace);
            }
            if (srsName.IsNotEmpty())
            {
                strReq.AppendFormat("&srs=" + srsName);
            }
            //Todo Hantera Parameter och parameterValue
            //
            strReq.AppendFormat("&outputFormat=" + outputFormat);

            return(strReq.ToString());
        }
Esempio n. 14
0
        /// <summary>
        /// Creates a WFS DescribeFeatureType request URL.
        /// </summary>
        /// <param name="strUrl">The WFS server URL.</param>
        /// <param name="typeNames">The features layer names you want info about.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static string CreateDescribeFeaturesRequestUrl(string strUrl, List <string> typeNames, WFSVersion version)
        {
            if (version == WFSVersion.Unknown)
            {
                throw new Exception("WFSVersion is unknown");
            }
            //XXX
            string strVersion = GetWFSVersionStringFromEnum(version);
            var    uri        = new Uri(strUrl);
            string baseUrl    = uri.GetLeftPart(UriPartial.Path);
            var    strReq     = new StringBuilder(baseUrl);

            strReq.Append("?");
            strReq.AppendFormat("service=wfs&");
            strReq.AppendFormat("request=DescribeFeatureType&");
            strReq.AppendFormat("TypeName=" + string.Join(",", typeNames) + "&");
            strReq.AppendFormat("version=" + strVersion);

            return(strReq.ToString());
        }
Esempio n. 15
0
        /// <summary>
        /// Creates a WFS DescribeFeatureType request URL.
        /// </summary>
        /// <param name="strUrl">The WFS server URL.</param>
        /// <param name="typeNames">The features layer names you want info about.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static string CreateDescribeFeaturesRequestUrl(string strUrl, List <WfsTypeName> typeNames, WFSVersion version)
        {
            var strTypeNames = typeNames.Select(typeName => typeName.FullName).ToList();

            return(CreateDescribeFeaturesRequestUrl(strUrl, strTypeNames, version));
        }
Esempio n. 16
0
 /// <summary>
 /// Creates a WFS DescribeFeatureType request URL.
 /// </summary>
 /// <param name="strUrl">The WFS server URL.</param>
 /// <param name="namespace">The namespace of the feature layer you want info about.</param>
 /// <param name="name">The name of the feature layer you want info about.</param>
 /// <param name="version">The WFS version.</param>
 /// <returns></returns>
 public static string CreateDescribeFeatureTypeRequestUrl(string strUrl, string @namespace, string name, WFSVersion version)
 {
     return(CreateDescribeFeatureTypeRequestUrl(strUrl, string.Format("{0}:{1}", @namespace, name), version));
 }
Esempio n. 17
0
 /// <summary>
 /// Parses a WFS response XML into a Dictionary where the key is the full name of the feature
 /// and the value is its corresponding WFSDescribeFeature
 /// </summary>
 /// <param name="strXml">WFS response XML.</param>
 /// <param name="version">The WFS version.</param>
 /// <returns></returns>
 public static Dictionary <string, WFSDescribeFeatureType> ParseDescribeFeatureTypes(string strXml, WFSVersion version)
 {
     return(WFSDescribeFeatureParser.ParseDescribeFeatureTypes(strXml));
 }
Esempio n. 18
0
        /// <summary>
        /// Makes a GetCapabilities request and a DescribeFeature request
        /// The feature types that we get from DescribeFeature is added to
        /// the WfsCapabilities object.
        /// </summary>
        /// <param name="url">
        /// The WFS Server URL.
        /// </param>
        /// <param name="version">
        /// WFS Version.
        /// </param>
        /// <returns>
        /// The <see cref="WFSCapabilities"/>.
        /// </returns>
        public static WFSCapabilities GetWFSCapabilitiesAndMergeDescribeFeatureTypes(string url, WFSVersion version)
        {
            WFSCapabilities wfsCapabilities = GetWFSCapabilities(url, version);
            Dictionary <string, WFSDescribeFeatureType> dicDescribeFeatureTypes = GetWFSDescribeFeatureTypes(url, wfsCapabilities.FeatureTypes, version);

            //List<WFSDescribeFeatureType> describeFeatureTypes = dicDescribeFeatureTypes.Values.ToList();

            foreach (var featureType in wfsCapabilities.FeatureTypes)
            {
                WFSDescribeFeatureType describeFeatureType;
                if (dicDescribeFeatureTypes.TryGetValue(featureType.Name.FullName, out describeFeatureType))
                {
                    featureType.DescribeFeatureType = describeFeatureType;
                }
            }

            return(wfsCapabilities);
        }
Esempio n. 19
0
        /// <summary>
        /// Makes a WFS DescribeFeatureType Request and returns a WFSDescribeFeature.
        /// </summary>
        /// <param name="url">The WFS server URL.</param>
        /// <param name="typeName">The feature layer name including namespace</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static WFSDescribeFeatureType GetWFSDescribeFeatureType(string url, string typeName, WFSVersion version)
        {
            string requestUrl = CreateDescribeFeatureTypeRequestUrl(url, typeName, version);

            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                string strXml = wc.DownloadString(requestUrl);
                WFSDescribeFeatureType result = ParseDescribeFeatureType(strXml, version);
                result.Name = new WfsTypeName(typeName);
                return(result);
                //return ParseDescribeFeatureType(strXml, version);
            }
        }
Esempio n. 20
0
 /// <summary>
 /// Makes a WFS DescribeFeatureType Request and returns a WFSDescribeFeature.
 /// </summary>
 /// <param name="url">The WFS server URL.</param>
 /// <param name="name">The feature layer name</param>
 /// <param name="version">The WFS version.</param>
 /// <param name="namespace">The feature layer namespace</param>
 /// <returns></returns>
 public static WFSDescribeFeatureType GetWFSDescribeFeatureType(string url, string @namespace, string name, WFSVersion version)
 {
     return(GetWFSDescribeFeatureType(url, string.Format("{0}:{1}", @namespace, name), version));
 }
Esempio n. 21
0
 /// <summary>
 /// Makes a WFS DescribeFeatureType Request and returns a WFSDescribeFeature.
 /// </summary>
 /// <param name="url">The WFS server URL.</param>
 /// <param name="featureType">The feature layer to get information about.</param>
 /// <param name="version">The WFS version.</param>
 /// <returns></returns>
 public static WFSDescribeFeatureType GetWFSDescribeFeatureType(string url, WfsFeatureType featureType, WFSVersion version)
 {
     return(GetWFSDescribeFeatureType(url, string.Format("{0}:{1}", featureType.Name.Namespace, featureType.Name), version));
 }
Esempio n. 22
0
        /// <summary>
        /// Makes a WFS DescribeFeatureType Request and return a dictionary where the key is the
        /// full name of the feature layer and the value is its corresponding WFSDescribeFeature
        /// </summary>
        /// <param name="url">The WFS server URL.</param>
        /// <param name="featureTypes">The feature layers to get information about.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static Dictionary <string, WFSDescribeFeatureType> GetWFSDescribeFeatureTypes(string url, List <WfsFeatureType> featureTypes, WFSVersion version)
        {
            List <WfsTypeName> typeNames = featureTypes.Select(featureType => featureType.Name).ToList();

            return(GetWFSDescribeFeatureTypes(url, typeNames, version));
        }
Esempio n. 23
0
 /// <summary>
 /// Parses a WFS response XML into a WFSDescribeFeature object
 /// </summary>
 /// <param name="strXml">WFS response XML.</param>
 /// <param name="version">The WFS version.</param>
 /// <returns></returns>
 public static WFSDescribeFeatureType ParseDescribeFeatureType(string strXml, WFSVersion version)
 {
     return(WFSDescribeFeatureParser.ParseDescribeFeatureType(strXml));
 }
Esempio n. 24
0
        /// <summary>
        /// Makes WFS DescribeFeatureType Request and return a dictionary where the key is the
        /// full name of the feature layer and the value is its corresponding WFSDescribeFeature
        /// </summary>
        /// <param name="url">The WFS server URL.</param>
        /// <param name="typeNames">The feature layers to get information about.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static Dictionary <string, WFSDescribeFeatureType> GetWFSDescribeFeatureTypes(string url, List <WfsTypeName> typeNames, WFSVersion version)
        {
            var dicDescribeFeaturesResult = new Dictionary <string, WFSDescribeFeatureType>();

            Dictionary <string, List <WfsTypeName> > dicTypeNames = GetTypeNamesByNamespace(typeNames);

            foreach (List <WfsTypeName> typeNameList in dicTypeNames.Values)
            {
                List <List <WfsTypeName> > validTypeNameLists = GetTypeNameListsOfValidSize(typeNameList);

                foreach (List <WfsTypeName> validTypeNameList in validTypeNameLists)
                {
                    string requestUrl = CreateDescribeFeaturesRequestUrl(url, validTypeNameList, version);

                    using (WebClient wc = new WebClient())
                    {
                        wc.Encoding = Encoding.UTF8;
                        string strXml = wc.DownloadString(requestUrl);
                        Dictionary <string, WFSDescribeFeatureType> dicDescribeFeature = ParseDescribeFeatureTypes(strXml, version);
                        foreach (KeyValuePair <string, WFSDescribeFeatureType> pair in dicDescribeFeature)
                        {
                            if (!dicDescribeFeaturesResult.ContainsKey(pair.Key))
                            {
                                dicDescribeFeaturesResult.Add(pair.Key, pair.Value);
                            }
                        }
                    }
                }
            }
            return(dicDescribeFeaturesResult);
        }
Esempio n. 25
0
        /// <summary>
        /// Makes a GetCapabilities request and a DescribeFeature request
        /// The feature type that we get from DescribeFeature is added to
        /// the WfsCapabilities object.
        /// </summary>
        /// <param name="url">The WFS Server URL.</param>
        /// <param name="typeName">The feature layer name including namespace.</param>
        /// <param name="version">The WFS version.</param>
        /// <returns></returns>
        public static WFSCapabilities GetWFSCapabilitiesAndMergeDescribeFeatureType(string url, string typeName, WFSVersion version)
        {
            WFSCapabilities        wfsCapabilities     = GetWFSCapabilities(url, version);
            WFSDescribeFeatureType describeFeatureType = GetWFSDescribeFeatureType(url, typeName, version);

            foreach (var featureType in wfsCapabilities.FeatureTypes)
            {
                if (featureType.Name.FullName == describeFeatureType.Name.FullName)
                {
                    featureType.DescribeFeatureType = describeFeatureType;
                }
            }

            return(wfsCapabilities);
        }