Example #1
0
        /// <summary>
        /// 连接地图服务,获取IAGSServerObjectName对象
        /// </summary>
        /// <param name="hostOrUrl">服务地址,例:http://services.arcgisonline.com/ArcGIS/services</param>
        /// <param name="serviceName">服务名称,例:ESRI_Imagery_World_2D</param>
        /// <param name="isLAN">是否局域网(Local Area Network)</param>
        /// <returns></returns>
        private IAGSServerObjectName GetServerObjectName(string hostOrUrl, string serviceName, bool isLAN = false)
        {
            //设置连接属性
            IPropertySet propertySet = new PropertySetClass();

            propertySet.SetProperty(isLAN ? "machine" : "url", hostOrUrl);

            //打开连接
            IAGSServerConnectionFactory factory    = new AGSServerConnectionFactory();
            IAGSServerConnection        connection = factory.Open(propertySet, 0);

            IAGSEnumServerObjectName serverObjectNames = connection.ServerObjectNames;

            serverObjectNames.Reset();
            IAGSServerObjectName serverObjectName;

            while ((serverObjectName = serverObjectNames.Next()) != null)
            {
                if (serverObjectName.Name.ToLower() == serviceName.ToLower() && serverObjectName.Type == "MapServer")
                {
                    break;
                }
            }
            return(serverObjectName);
        }
Example #2
0
        public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {
            //设置连接属性
            IPropertySet pPropertySet = new PropertySetClass();

            if (pIsLAN)
            {
                pPropertySet.SetProperty("machine", pHostOrUrl);
            }
            else
            {
                pPropertySet.SetProperty("url", pHostOrUrl);
            }

            //打开连接

            IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
            //Type factoryType = Type.GetTypeFromProgID(
            //    "esriGISClient.AGSServerConnectionFactory");
            //IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)
            //    Activator.CreateInstance(factoryType);
            IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);

            //Get the image server.
            IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;

            pServerObjectNames.Reset();
            IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();

            while (ServerObjectName != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
                    (ServerObjectName.Type == "MapServer"))
                {
                    break;
                }
                ServerObjectName = pServerObjectNames.Next();
            }

            //返回对象
            return(ServerObjectName);
        }
Example #3
0
        /// <summary>
        /// 获取ArcGisServer地图服务标识,连接服务器
        /// </summary>
        /// <param name="pHostOrUrl"></param>服务器主机URL
        /// <param name="pServiceName"></param>服务名称
        /// <param name="pIsLAN"></param>主机是否是在局域网或者是互联网
        /// <returns></returns>
        private IAGSServerObjectName GetAGSMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {
            //设置连接属性
            IPropertySet pPropertySet = new PropertySet();

            if (pIsLAN)
            {
                pPropertySet.SetProperty("machine", pHostOrUrl);
            }
            else
            {
                pPropertySet.SetProperty("url", pHostOrUrl);
            }

            IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
            IAGSServerConnection        agsConn  = pFactory.Open(pPropertySet, 0);

            //打开获取服务器
            IAGSEnumServerObjectName pServerObjectNames = agsConn.ServerObjectNames;

            //获取服务器上所有服务的标识属性,即服务标识集合
            pServerObjectNames.Reset();
            //使指针指向服务开头
            IAGSServerObjectName ServerObjectName = null;

            //获取服务标识
            while ((ServerObjectName = pServerObjectNames.Next()) != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
                    (ServerObjectName.Type == "MapServer"))
                {//判断获取所需服务
                    break;
                }
            }
            //返回对象
            return(ServerObjectName);//返回服务标识
        }
        private byte[] SampleOperHandler(NameValueCollection boundVariables,
                                                  JsonObject operationInput,
                                                      string outputFormat,
                                                      string requestProperties,
                                                  out string responseProperties)
        {
            responseProperties = null;

            string parm1Value;
            bool found = operationInput.TryGetString("parm1", out parm1Value);
            if (!found || string.IsNullOrEmpty(parm1Value))
                throw new ArgumentNullException("parm1");

            string parm2Value;
            found = operationInput.TryGetString("parm2", out parm2Value);
            if (!found || string.IsNullOrEmpty(parm2Value))
                throw new ArgumentNullException("parm2");


            string outputSTR = null;

            IPropertySet propertySet = new PropertySet();
            propertySet.SetProperty("URL", "http://localhost:6080/arcgis/admin");
            propertySet.SetProperty("USER", "yourServerAdminUsername") ; 
            propertySet.SetProperty("PASSWORD", "yourServerAdminPassword");
            propertySet.SetProperty("ConnectionMode", esriAGSConnectionMode.esriAGSConnectionModeAdmin);
            propertySet.SetProperty("ServerType", esriAGSServerType.esriAGSServerTypeDiscovery);
            IAGSServerConnectionFactory agsServerConnectionFactory = new AGSServerConnectionFactory();
            IAGSServerConnectionAdmin agsServerConnection = agsServerConnectionFactory.Open(propertySet, 0) as IAGSServerConnectionAdmin;
            IServerObjectAdmin servAdmin = agsServerConnection.ServerObjectAdmin;
            IEnumServerDirectory pEnumServDir = servAdmin.GetServerDirectories();
            pEnumServDir.Reset();
            IServerDirectory2 pServDir = pEnumServDir.Next() as IServerDirectory2;
            while (pServDir != null)
            {
                string cleaningMd = pServDir.CleaningMode.ToString(); //cleanup mode
                string descrip = pServDir.Description.ToString(); //Description
                int fileAge = pServDir.MaxFileAge; //Max File Age
                string path = pServDir.Path; //physical path
                string url = pServDir.URL; //virtual path
                string type = pServDir.Type.ToString();
                pServDir = pEnumServDir.Next() as IServerDirectory2;
                outputSTR = outputSTR + "cleanup mode: " + cleaningMd + "\n" + "Description: " + descrip + "\n" + "Max File Age: " + fileAge + "\n" + "physical path: " + path + "\n" + "virtual path: " + url + "\n" + "type: " + type.ToString() + "\r\n";
            }
            JsonObject result = new JsonObject();
            result.AddString("parm1", parm1Value);
            result.AddString("parm2", outputSTR);

            return Encoding.UTF8.GetBytes(result.ToJson());
        }
        private void AddAGSService(string fileName)
        {
            try
            {
                IMxDocument mxDoc = (IMxDocument)m_application.Document;
                IMap map = (IMap)mxDoc.FocusMap;
                IActiveView activeView = (IActiveView)map;

                bool isAlreadyInMap = false;

                if (isAlreadyInMap)
                {
                    ShowErrorMessageBox(StringResources.MapServiceLayerAlreadyExistInMap);
                    return;
                }
                else
                {
                    if (fileName.ToLower().Contains("http") && !fileName.ToLower().Contains("arcgis/rest"))
                    {
                        if(fileName.EndsWith("MapServer"))
                            fileName = fileName.Remove(fileName.LastIndexOf("MapServer"));

                        String[] s = fileName.ToLower().Split(new String[]{"/services"}, StringSplitOptions.RemoveEmptyEntries);

                        IPropertySet propertySet = new PropertySetClass();
                        propertySet.SetProperty("URL", s[0] + "/services"); // fileName

                       IMapServer mapServer = null;

                        IAGSServerConnectionFactory pAGSServerConFactory =  new AGSServerConnectionFactory();
                        IAGSServerConnection agsCon = pAGSServerConFactory.Open(propertySet,0);
                        IAGSEnumServerObjectName pAGSSObjs = agsCon.ServerObjectNames;
                        IAGSServerObjectName pAGSSObj = pAGSSObjs.Next();

                       while (pAGSSObj != null) {
                        if(pAGSSObj.Type=="MapServer" && pAGSSObj.Name.ToLower() == s[1].TrimStart('/').TrimEnd('/')){
                            break;
                        }
                        pAGSSObj = pAGSSObjs.Next();
                       }

                        IName pName =  (IName) pAGSSObj;
                        IAGSServerObject pAGSO = (IAGSServerObject) pName.Open();
                        mapServer = (IMapServer) pAGSO;

                        IPropertySet prop = new PropertySetClass();
                        prop.SetProperty("URL", fileName);
                        prop.SetProperty("Name",pAGSSObj.Name);

                        IMapServerLayer layer = new MapServerLayerClass();
                        layer.ServerConnect(pAGSSObj,mapServer.get_MapName(0));

                        mxDoc.AddLayer((ILayer)layer);

                    }
                    else
                    {

                        IGxFile pGxFile;

                        if (fileName.ToLower().EndsWith(".tif"))
                        {
                            IRasterLayer pGxLayer = (IRasterLayer)new RasterLayer();
                            pGxLayer.CreateFromFilePath(fileName);
                            if (pGxLayer.Valid)
                            {
                                map.AddLayer((ILayer)pGxLayer);
                            }
                        }
                        else
                        {
                            if (fileName.ToLower().Contains("http") && fileName.ToLower().Contains("arcgis/rest"))
                            {
                                String[] s = fileName.ToLower().Split(new String[] { "/rest" }, StringSplitOptions.RemoveEmptyEntries);

                                IPropertySet propertySet = new PropertySetClass();
                                propertySet.SetProperty("URL", s[0] + "/services"); // fileName

                                IMapServer mapServer = null;

                                IAGSServerConnectionFactory pAGSServerConFactory = new AGSServerConnectionFactory();
                                IAGSServerConnection agsCon = pAGSServerConFactory.Open(propertySet, 0);
                                IAGSEnumServerObjectName pAGSSObjs = agsCon.ServerObjectNames;
                                IAGSServerObjectName pAGSSObj = pAGSSObjs.Next();

                                String[] parts = s[1].ToLower().Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                                while (pAGSSObj != null)
                                {
                                    if (pAGSSObj.Type == "MapServer" && pAGSSObj.Name.ToLower() == parts[1])
                                    {
                                        break;
                                    }
                                    pAGSSObj = pAGSSObjs.Next();
                                }

                                IName pName = (IName)pAGSSObj;
                                IAGSServerObject pAGSO = (IAGSServerObject)pName.Open();
                                mapServer = (IMapServer)pAGSO;

                                IPropertySet prop = new PropertySetClass();
                                prop.SetProperty("URL", fileName);
                                prop.SetProperty("Name", pAGSSObj.Name);

                                IMapServerLayer layer = new MapServerLayerClass();
                                layer.ServerConnect(pAGSSObj, mapServer.get_MapName(0));

                                mxDoc.AddLayer((ILayer)layer);
                            }
                            else
                            {
                                IGxLayer pGxLayer = new GxLayer();
                                pGxFile = (GxFile)pGxLayer;
                                pGxFile.Path = fileName;

                                if (pGxLayer.Layer != null)
                                {
                                    map.AddLayer(pGxLayer.Layer);
                                }
                            }

                        }

                    }

                }
            }
            catch (Exception ex)
            {
                ShowErrorMessageBox(StringResources.AddArcGISLayerFailed + "\r\n" + ex.Message);
            }
        }
Example #6
0
        /// <summary>
        /// Add AGS Map Layer
        /// </summary>
        /// <param name="inUserName"></param>
        /// <param name="inPassword"></param>
        /// <param name="inName"></param>
        /// <param name="inMapUrl"></param>
        internal void AddAGSLayer(string inUserName, string inPassword, string inName, string inMapUrl)
        {
            IAGSServerConnectionName2 agsServerConnectionName = (IAGSServerConnectionName2) new AGSServerConnectionName();

            // http://localhost/ArcGIS/rest/services/Map1/MapServer
            // Convert to http://localhost/ArcGis/Services
            // and http://localgsot/ArcGIS/Map1/MapServer

            string servicecatalog = inMapUrl.Substring(0, inMapUrl.IndexOf("/REST/SERVICES", StringComparison.CurrentCultureIgnoreCase)) + "/Services";
            string mapserviceurl  = ReplaceEx(inMapUrl, "/REST/", "/", StringComparison.InvariantCultureIgnoreCase);
            string mapservice     = "";
            Regex  rMapServerRest = new Regex("^(?i:.*/REST/SERVICES/(?<mapname>.*)/MAPSERVER.*)$");
            Match  m = rMapServerRest.Match(inMapUrl);

            if (m.Success)
            {
                mapservice = m.Groups["mapname"].Value;
            }


            //Create a property set of connection details
            IPropertySet props = new PropertySet();

            props.SetProperty("url", servicecatalog);
            if (inUserName != null)
            {
                props.SetProperty("user", inUserName);
                props.SetProperty("password", inPassword);
                props.SetProperty("hideuserproperty", false);
                props.SetProperty("anonymous", false);
            }
            IAGSServerObjectName2 agsServerObjectName = (IAGSServerObjectName2) new AGSServerObjectName();

            IAGSServerConnectionFactory AGSConnectionFactory = new AGSServerConnectionFactory();

            IAGSServerConnection     AGSConnection = AGSConnectionFactory.Open(props, 0);
            IAGSEnumServerObjectName enumSOName    = null;



            enumSOName = AGSConnection.ServerObjectNames;
            IAGSServerObjectName SOName = null;

            SOName = enumSOName.Next();
            while (SOName != null)
            {
                System.Diagnostics.Debug.WriteLine(SOName.Name + ": " + SOName.Type);
                if (SOName.Name.Equals(mapservice, StringComparison.InvariantCultureIgnoreCase) && SOName.Type == "MapServer")
                {
                    //create the layer object
                    IMapServerGroupLayer mapServerLayer = new MapServerLayerClass();
                    IName      mapServerConnectionName  = (IName)SOName;
                    IDataLayer dataLayer = (IDataLayer)mapServerLayer;
                    //try to connect
                    try
                    {
                        dataLayer.Connect(mapServerConnectionName);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Could not connect to URL", ex);
                    }

                    //create the layer name
                    ILayer layer;

                    layer         = (ILayer)mapServerLayer;
                    layer.Visible = true;
                    layer.Name    = inName;

                    //return the layer
                    ArcMap.Document.AddLayer(layer);
                    break;
                }
                SOName = enumSOName.Next();
            }
        }
Example #7
0
        /// <summary>
        /// Adds ArcGIS layer to map
        /// </summary>
        /// <param name="ipMxDoc"></param>
        /// <param name="strServer"></param>
        /// <param name="strLayerName"></param>
        /// <param name="strSecretName"></param>
        /// <returns></returns>
        private bool addLayerAGS(IMxDocument ipMxDoc, string strServer, string strLayerName, string strSecretName)
        {
            IPropertySet2 pProps = null;
            string pServerUrl;
            string strServerObj;

            pServerUrl = GetAGSServiceUrl(strServer);

            // connect to the GIS server
            IAGSServerConnectionFactory pAGSServerConnectionFactory = new AGSServerConnectionFactory();
            pProps = (IPropertySet2)new PropertySet();
            pProps.SetProperty("URL", pServerUrl);

            IAGSServerConnection pAGSConnection = pAGSServerConnectionFactory.Open(pProps, 0);

            //get server objectname from url
            strServerObj = GetServerObjectName(strServer);

            // enumerate over server objects
            IAGSEnumServerObjectName pAGSSObjs = pAGSConnection.ServerObjectNames;
            IAGSServerObjectName pAGSSObj = pAGSSObjs.Next();

            while (pAGSSObj != null)
            {
                if (pAGSSObj.Type == "MapServer" && pAGSSObj.Name == strServerObj)
                {
                    break;
                }
                pAGSSObj = pAGSSObjs.Next();
            }

            IName pName = (IName)pAGSSObj;
            IAGSServerObject pAGSO = (IAGSServerObject)pName.Open();
            IMapServer mapServer = (IMapServer)pAGSO;

            IPropertySet prop = new PropertySetClass();
            prop.SetProperty("URL", pServerUrl);
            prop.SetProperty("Name", pAGSSObj.Name);

            //Create new layer
            IMapServerLayer pMSLayer = (IMapServerLayer)new MapServerLayer();
            pMSLayer.ServerConnect(pAGSSObj, mapServer.DefaultMapName);

            if (!isMapServerAdded(strServer))
            {
                setAGSLayerVisiblity((MapServerLayer)pMSLayer, strSecretName);
                IMap ipMap = ipMxDoc.FocusMap;
                ipMap.AddLayer((ILayer)pMSLayer);
                logger.writeLog(StringResources.AddAGSLayer + strSecretName);
            }
            else
            {
                // set visibility
                setAGSLayerVisiblity((MapServerLayer)pMSLayer, strSecretName);
            }

            //add to the service list
            string strItem = EncodeServiceList(strServer, strLayerName, strSecretName);

            // m_pLogger.Msg "adding to service list : " & strItem
            colServiceList.Add(strItem);

            //set flag
            logger.writeLog("strServer = " + strServer + " strServer & strLayerName = " + strServer + strLayerName);
            return true;
        }
Example #8
0
        /// <summary>
        /// Adds ArcGIS layer to map
        /// </summary>
        /// <param name="ipMxDoc"></param>
        /// <param name="strServer"></param>
        /// <param name="strLayerName"></param>
        /// <param name="strSecretName"></param>
        /// <returns></returns>
        private bool addLayerAGS(IMxDocument ipMxDoc, string strServer, string strLayerName, string strSecretName)
        {
            IPropertySet2 pProps = null;
            string        pServerUrl;
            string        strServerObj;

            pServerUrl = GetAGSServiceUrl(strServer);

            // connect to the GIS server
            IAGSServerConnectionFactory pAGSServerConnectionFactory = new AGSServerConnectionFactory();

            pProps = (IPropertySet2) new PropertySet();
            pProps.SetProperty("URL", pServerUrl);

            IAGSServerConnection pAGSConnection = pAGSServerConnectionFactory.Open(pProps, 0);

            //get server objectname from url
            strServerObj = GetServerObjectName(strServer);

            // enumerate over server objects
            IAGSEnumServerObjectName pAGSSObjs = pAGSConnection.ServerObjectNames;
            IAGSServerObjectName     pAGSSObj  = pAGSSObjs.Next();

            while (pAGSSObj != null)
            {
                if (pAGSSObj.Type == "MapServer" && pAGSSObj.Name == strServerObj)
                {
                    break;
                }
                pAGSSObj = pAGSSObjs.Next();
            }


            IName            pName     = (IName)pAGSSObj;
            IAGSServerObject pAGSO     = (IAGSServerObject)pName.Open();
            IMapServer       mapServer = (IMapServer)pAGSO;


            IPropertySet prop = new PropertySetClass();

            prop.SetProperty("URL", pServerUrl);
            prop.SetProperty("Name", pAGSSObj.Name);

            //Create new layer
            IMapServerLayer pMSLayer = (IMapServerLayer) new MapServerLayer();

            pMSLayer.ServerConnect(pAGSSObj, mapServer.DefaultMapName);

            if (!isMapServerAdded(strServer))
            {
                setAGSLayerVisiblity((MapServerLayer)pMSLayer, strSecretName);
                IMap ipMap = ipMxDoc.FocusMap;
                ipMap.AddLayer((ILayer)pMSLayer);
                logger.writeLog(StringResources.AddAGSLayer + strSecretName);
            }
            else
            {
                // set visibility
                setAGSLayerVisiblity((MapServerLayer)pMSLayer, strSecretName);
            }

            //add to the service list
            string strItem = EncodeServiceList(strServer, strLayerName, strSecretName);

            // m_pLogger.Msg "adding to service list : " & strItem
            colServiceList.Add(strItem);

            //set flag
            logger.writeLog("strServer = " + strServer + " strServer & strLayerName = " + strServer + strLayerName);
            return(true);
        }