/// <summary>
        /// Adds WMS layer to map
        /// </summary>
        /// <param name="msi">map service information</param>
        /// <param name="fromServerUrl">service url</param>
        /// <returns></returns>
        private bool addLayerWMS2(MapServiceInfo msi, Boolean fromServerUrl)
        {
            bool flag = false;
            if (msi == null) { throw new ArgumentNullException("msi"); }
            string service = msi.Service;
            string url = AppendQuestionOrAmpersandToUrlString(msi.Server);
            // append serviceParam to server url?
            if (msi.ServiceParam.Length > 0 && !fromServerUrl)
            {
                url = url + msi.ServiceParam;
                url = AppendQuestionOrAmpersandToUrlString(url);
            }


            CswClient client = new CswClient();
            string response = client.SubmitHttpRequest("GET", url + "request=GetCapabilities&service=WMS", "");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(response);
            XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlnsManager.AddNamespace("wms", "http://www.opengis.net/wms");
            XmlNodeList nl = null;
            if (xmlDoc.SelectSingleNode("//wms:Layer", xmlnsManager) != null)
            {
                nl = xmlDoc.SelectNodes("/wms:WMS_Capabilities/wms:Capability/wms:Layer/wms:Layer/wms:Title", xmlnsManager);

            }

            if (nl != null)
            {
                flag = true;
                for (int i = nl.Count - 1; i >= 0; i--)
                {

                    AGXD.ServiceConnectionProperties conn = new AGXD.ServiceConnectionProperties
                        (AGXD.ServiceType.Wms,
                        new Uri(url),
                        "", nl.Item(i).InnerText);

                    AGXM.ServiceLayer sl = new AGXM.ServiceLayer(conn);

                    bool connected = sl.Connect();

                    if (connected)
                    {
                        addLayer(sl);
                        addedLayer.Add(DateTime.Now.Millisecond, sl);
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// Function to add to map
        /// </summary>
        private void AddToMap_Clicked()
        {
            try
            {

                /*    if (addedLayer[lstSearchResults.SelectedItem.GetHashCode()] != null) {
                        AGXM.ServiceLayer resultLayer = (AGXM.ServiceLayer)addedLayer[lstSearchResults.SelectedItem.GetHashCode()];
                        try {
                            if (resultLayer.Extent.GetEnvelope() != null) {
                                MessageBox.Show(resourceManager.GetString("layerAdded"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        catch (Exception e) {
                            addedLayer.Remove(lstSearchResults.SelectedItem.GetHashCode());
                        }
                    }*/
                //     if (addedLayer[lstSearchResults.SelectedItem.GetHashCode()] == null) {
                Cursor.Current = Cursors.WaitCursor;
                // retrieve metadata

                CswRecord record = (CswRecord)lstSearchResults.SelectedItem;
                if (record == null) throw new NullReferenceException("CswRecord is null.");

                if (record.MapServerURL == null || record.MapServerURL.Trim().Length == 0)
                {
                    // retrieve metadata
                    RetrieveAddToMapInfoFromCatalog();
                }
                else
                {
                    _mapServerUrl = record.MapServerURL;
                }
                //  if (xmlDoc == null) return;

                // prepare metadata for service info
                //  xmlDoc = PrepareMetadata(xmlDoc);
                //   if (xmlDoc == null) return;

                if (_mapServerUrl != null && _mapServerUrl.Trim().Length > 0)
                {
                    String serviceType = record.ServiceType;
                    if (serviceType == null || serviceType.Length == 0)
                    {
                        serviceType = CswProfile.getServiceType(_mapServerUrl);
                    }

                    if (serviceType.Equals("ags"))
                    {
                        if (_mapServerUrl.ToLower().Contains("arcgis/rest"))
                        {
                            _mapServerUrl = _mapServerUrl + "?f=nmf";
                            CswClient client = new CswClient();
                            AddAGSService(client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, ""));
                        }
                        else
                        {
                            AddAGSService(_mapServerUrl);
                        }
                    }
                    else if (serviceType.Equals("wms") || serviceType.Equals("aims"))
                    {
                        try
                        {
                            MapServiceInfo msinfo = new MapServiceInfo();
                            msinfo.Server = record.MapServerURL;
                            msinfo.Service = record.ServiceName;
                            msinfo.ServiceType = record.ServiceType;
                            CswProfile.ParseServiceInfoFromUrl(msinfo, _mapServerUrl, serviceType);
                            addMapServiceLayer(msinfo);

                        }
                        catch (Exception e)
                        {
                            AddAGSService(_mapServerUrl);
                        }

                    }

                }
                else
                {
                    MapServiceInfo msi = new MapServiceInfo();

                    // parse out service information            
                    //   ParseServiceInfoFromMetadata(xmlDoc, ref msi);

                    if (msi.IsMapService())
                    {
                        addMapServiceLayer(msi);
                    }
                    else
                    {
                        MessageBox.Show(resourceManager.GetString("invalidService"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                //  }

            }
            catch (Exception ex)
            {
                MessageBox.Show(resourceManager.GetString("addMapFailed"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

        }
        /// <summary>
        /// Retrieve metadta for the selected record from server. 
        /// Then add the live data or maps that the metadta describes to ArcMap as a layer.
        /// </summary>
        private void AddToMap_Clicked()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                CswRecord record = (CswRecord)resultsListBox.SelectedItem;
                if (record == null) throw new NullReferenceException(StringResources.CswRecordIsNull);

                if (record.MapServerURL == null || record.MapServerURL.Trim().Length == 0)
                {
                    // retrieve metadata
                    RetrieveAddToMapInfoFromCatalog();
                }
                else
                {
                    _mapServerUrl = record.MapServerURL;

                }

                if (_mapServerUrl != null && _mapServerUrl.Trim().Length > 0)
                {

                    String serviceType = record.ServiceType;
                    if (serviceType == null || serviceType.Length == 0)
                    {
                        serviceType = CswProfile.getServiceType(_mapServerUrl);
                    }
                    if (serviceType.Equals("unknown"))
                    {
                        System.Diagnostics.Process.Start("IExplore", _mapServerUrl);
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    else if (serviceType.Equals("ags"))
                    {
                        if (_mapServerUrl.ToLower().Contains("arcgis/rest"))
                        {
                            var urlparts = _mapServerUrl.Split('/');
                            if (urlparts != null && urlparts.Length > 0)
                            {
                                var lastPartOfUrl = urlparts[urlparts.Length - 1];
                                if (lastPartOfUrl.Length > 0 && IsNumeric(lastPartOfUrl))
                                {
                                   // CswClient client = new CswClient();
                                    AddAGSService( _mapServerUrl);
                                }
                                else
                                {
                                    _mapServerUrl = _mapServerUrl + "?f=lyr";
                                    CswClient client = new CswClient();
                                    AddAGSService(client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, ""));
                                }
                            }
                        }
                        else
                        {
                            AddAGSService(_mapServerUrl);
                        }
                    }
                    else if (serviceType.Equals("wms"))
                    {
                        MapServiceInfo msinfo = new MapServiceInfo();
                        msinfo.Server = record.MapServerURL;
                        msinfo.Service = record.ServiceName;
                        msinfo.ServiceType = record.ServiceType;
                        CswProfile.ParseServiceInfoFromUrl(msinfo,_mapServerUrl, serviceType);
                        AddLayerWMS(msinfo, true);
                    }
                    else if (serviceType.Equals("aims"))
                    {
                        MapServiceInfo msinfo = new MapServiceInfo();
                        msinfo.Server = record.MapServerURL;
                        msinfo.Service = record.ServiceName;
                        msinfo.ServiceType = record.ServiceType;
                        CswProfile.ParseServiceInfoFromUrl(msinfo,_mapServerUrl, serviceType);
                        AddLayerArcIMS(msinfo);
                    }
                    else if (serviceType.Equals("wcs"))
                    {
                        // MapServiceInfo msi = new MapServiceInfo();
                        String[] s = _mapServerUrl.Trim().Split('?');

                        _mapServerUrl = s[0] + "?request=GetCapabilities&service=WCS";
                        CswClient client = new CswClient();
                        String response = client.SubmitHttpRequest("GET", _mapServerUrl, "");

                        XmlDocument xmlDocument = new XmlDocument();
                        try { xmlDocument.LoadXml(response); }
                        catch (XmlException xmlEx)
                        { }

                        XmlNodeList contentMetadata = xmlDocument.GetElementsByTagName("ContentMetadata");

                        if (contentMetadata != null && contentMetadata.Count > 0)
                        {
                            XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes;

                            foreach (XmlNode coverage in coverageList)
                            {

                                XmlNodeList nodes = coverage.ChildNodes;

                                foreach (XmlNode node in nodes)
                                {
                                    if (node.Name.ToLower().Equals("name"))
                                    {
                                        _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + node.InnerText;

                                        try
                                        {
                                            String filePath = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "");
                                            AddAGSService(filePath);

                                        }
                                        catch (Exception e)
                                        {
                                            ShowErrorMessageBox(StringResources.wcsWithNoGeoTiffInterface);
                                            return;
                                        }
                                    }
                                }

                            }

                        }
                        else
                        {
                            ShowErrorMessageBox(StringResources.serviceTypeUnknown);
                            return;

                          /*  contentMetadata = xmlDocument.GetElementsByTagName("CoverageSummary");

                            if (contentMetadata != null && contentMetadata.Count > 0)
                            {
                                XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes;

                                foreach (XmlNode coverage in coverageList)
                                {

                                    if (coverage.Name.ToLower().Equals("identifier"))
                                    {
                                        _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + coverage.InnerText;

                                        try
                                        {
                                            String filePath = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "");
                                            AddAGSService(filePath);

                                        }
                                        catch (Exception e)
                                        {
                                            ShowErrorMessageBox("WCS service with no GeoTiff interface");
                                            return;
                                        }
                                    }
                                }

                            }*/

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessageBox(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        /// <summary>
        /// Add WCS map service layer to map
        /// </summary>
        /// <param name="msi">Map service information</param>
        private void AddLayerWCS(esri.gpt.csw.MapServiceInfo msi, Boolean fromServerUrl)
        {
            if (msi == null) { throw new ArgumentNullException(); }

            try
            {
                string _mapServerUrl = AppendQuestionOrAmpersandToUrlString(msi.Server);
                // append serviceParam to server url
                // todo: does msi.ServiceParam have a leading "?" or "&"?
                if (msi.ServiceParam.Length > 0 && !fromServerUrl)
                {
                    _mapServerUrl = _mapServerUrl + msi.ServiceParam;
                    _mapServerUrl = AppendQuestionOrAmpersandToUrlString(_mapServerUrl);
                }

                 // MapServiceInfo msi = new MapServiceInfo();
                        String[] s = _mapServerUrl.Trim().Split('?');

                        _mapServerUrl = s[0] + "?request=GetCapabilities&service=WCS";
                        CswClient client = new CswClient();
                        String response = client.SubmitHttpRequest("GET", _mapServerUrl, "");

                         XmlDocument xmlDocument = new XmlDocument();
                         try { xmlDocument.LoadXml(response); }
                         catch (XmlException xmlEx)
                         { }

                         XmlNodeList contentMetadata = xmlDocument.GetElementsByTagName("ContentMetadata");

                         if (contentMetadata != null && contentMetadata.Count > 0)
                         {
                             XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes;

                             foreach (XmlNode coverage in coverageList) {

                                 XmlNodeList nodes = coverage.ChildNodes;

                                 foreach(XmlNode node in nodes)
                                 {
                                     if (node.Name.ToLower().Equals("name"))
                                     {
                                         _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + node.InnerText;

                                         try{
                                            String filePath  = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "");
                                            AddAGSService(filePath);

                                         } catch(Exception e){
                                             ShowErrorMessageBox("WCS service with no GeoTiff interface");
                                             return;
                                         }
                                     }
                                 }

                             }

                         }

                    }

            catch (Exception ex)
            {
              //  ShowErrorMessageBox(StringResources.AddWcsLayerFailed + "\r\n" + ex.Message);
            }
        }
        /// <summary>
        /// Search CSW catalog using the provided criteria. Search result can be accessed by calling GetResponse().
        /// </summary>
        public void Search()
        {
            _response = new CswSearchResponse();
            StringBuilder sb = new StringBuilder();

            if (_criteria == null)
            {
                sb.AppendLine(DateTime.Now + " Criteria not specified.");
                throw new NullReferenceException("Criteria not specified.");
            }
            if (_catalog == null)
            {
                sb.AppendLine(DateTime.Now + " Catalog not specified.");
                throw new NullReferenceException("Catalog not specified.");
            }
            if (_catalog.URL == null || _catalog.URL.Length == 0)
            {
                sb.AppendLine(DateTime.Now + " Catalog URL not specified.");
                throw new NullReferenceException("Catalog URL not specified.");
            }
            if (_catalog.Profile == null)
            {
                sb.AppendLine(DateTime.Now + " Catalog profile not specified.");
                throw new NullReferenceException("Catalog profile not specified.");
            }

            CswProfile profile = _catalog.Profile;

            writeLogMessage("Csw profile used : " + profile.Name);
            // generate getRecords query
            string requestUrl   = "";
            string requestQuery = "";

            if (_catalog.Profile.isOGCRecords)
            {
                requestUrl   = _catalog.URL;
                requestQuery = "f=json&q=" + _criteria.SearchText;
            }
            else
            {
                requestUrl   = _catalog.Capabilities.GetRecords_PostURL;
                requestQuery = profile.GenerateCSWGetRecordsRequest(_criteria);
            }

            //   requestQuery = "<csw:GetCapabilities xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" request=\"GetCapabilities\" service=\"CSW\" version=\"2.0.2\"/>";

            requestQuery = requestQuery.Replace("utf-16", "utf-8");
            requestQuery = requestQuery.Replace("UTF-16", "UTF-8");

            // submit search query
            if (_cswClient == null)
            {
                _cswClient = new CswClient();
            }

            string responseText;

            sb.AppendLine(DateTime.Now + " Sending CSW GetRecords request to endpoint : " + requestUrl);
            sb.AppendLine("Request Query : " + requestQuery);
            if (_catalog.Profile.isOGCRecords)
            {
                responseText = _cswClient.SubmitHttpRequest("GET", requestUrl, requestQuery);
            }
            else
            {
                if (!_catalog.Capabilities.GetRecords_IsSoapEndPoint)
                {
                    responseText = _cswClient.SubmitHttpRequest("POST", requestUrl, requestQuery);
                }
                else
                {
                    responseText = _cswClient.SubmitHttpRequest("SOAP", requestUrl, requestQuery);
                }
            }

            // parse out csw search records
            CswRecords records = new CswRecords();

            sb.AppendLine(DateTime.Now + " Response received : " + responseText);
            if (_catalog.Profile.isOGCRecords)
            {
                profile.ReadOGCAPIRecordsResponse(responseText, records);
            }
            else
            {
                profile.ReadCSWGetRecordsResponse(responseText, records);
            }
            sb.AppendLine(DateTime.Now + " Parsed GetRecords response.");

            // populate CSW response
            _response.ResponseXML = responseText;
            _response.Records     = records;

            writeLogMessage(sb.ToString());
        }
Esempio n. 6
0
            /// <summary>
            ///  To retrieve informations about the CSW service.
            /// </summary>
            /// <remarks>
            /// </remarks>
            /// <param name="param1">capabilities baseurl</param>
            /// <returns>Response the get capabilities url</returns>
            private string GetCapabilities(string capabilitiesurl) {
                try {
                    CswClient client = new CswClient();
                    Utils.logger.writeLog("GetCapabilities url : " + capabilitiesurl);
                    string response = client.SubmitHttpRequest("GET", capabilitiesurl, "");
                    Utils.logger.writeLog("GetCapabilities response : " + response);
                    //Console.WriteLine()
                    

                    XmlDocument xmlDoc = new XmlDocument();
                    if (response == null)
                        return null ;
                    xmlDoc.LoadXml(response); 
                    XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
                    if (this.Profile.CswNamespace.Length <= 0) {
                        this.Profile.CswNamespace = CswProfiles.DEFAULT_CSW_NAMESPACE;
                    }
                    xmlnsManager.AddNamespace("ows", "http://www.opengis.net/ows");
                    xmlnsManager.AddNamespace("csw", this.Profile.CswNamespace);
                    xmlnsManager.AddNamespace("wrs10", "http://www.opengis.net/cat/wrs/1.0");
                    xmlnsManager.AddNamespace("wrs", "http://www.opengis.net/cat/wrs");
                    xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
                    xmlnsManager.AddNamespace("wcs", "http://www.opengis.net/wcs");
                    if (xmlDoc.SelectSingleNode("/csw:Capabilities|/wrs:Capabilities| /wrs10:Capabilities | /wcs:WCS_Capabilities", xmlnsManager) != null)
                        return response;
                    else
                        return null;
                }
                catch (Exception ex) {
                    Utils.logger.writeLog(ex.StackTrace);
                    throw ex;
                }
            }