使用.Net操作 Geoserver REST API的shpfile发布

因为Geoserver REST支持Http的GET/PUT/POST请求,所以可以在C#中可以通过创建Http请求来达到操作Geoserver的目的。下面是代码:

        private static readonly string contentType = "text/xml";
        private static readonly string userName = "admin";        //geoserver账号
        private static readonly string password = "geoserver";      //geoserver密码
        private static readonly string geoserver = "http://localhost:8080/geoserver";      //geoserver的url

        private static void Create() 
        {
            string workspaceName = "TestWorkSpace";    //添加数据的工作区
            string storeName = "TestPoint";            //添加的数据显示名称
            string filePath = @"D:\TestFile\TestPoint.shp";         //需要添加的数据源
            string str = CreateWorkSpace(workspaceName);         //创建工作区
            str = CreateStore(workspaceName, filePath, storeName);       //在存在的工作区创建数据源
            str = PublishStore(workspaceName, storeName, null);
        }

        /// <summary>
        /// 创建geoserver新工作区
        /// </summary>
        /// <param name="workspaceName"></param>
        /// <returns></returns>
        private static string CreateWorkSpace(string workspaceName)
        {
            string url = geoserver + "/rest/workspaces";
            string xmlString = "<workspace><name>" + workspaceName + "</name></workspace>";
            string str = "";
            try
            {
                str = CreatePost(url, xmlString, Encoding.UTF8);
                if (str == workspaceName)
                    return "工作区创建成功";
                else
                    return "工作区创建成功";
            }
            catch (Exception ex)
            {
                return "工作区创建失败:"+ex.Message;
            }
        }

        /// <summary>
        /// 创建geoserver新数据源
        /// </summary>
        /// <param name="workspaceName"></param>
        /// <param name="filePath"></param>
        /// <param name="storeName"></param>
        /// <returns></returns>
        private static string CreateStore(string workspaceName, string filePath, string storeName)
        {
            string fileUrl = "file://" + filePath;
            string url = geoserver + "/rest/workspaces/" + workspaceName + "/datastores";
            string xmlString = "<dataStore><name>" + storeName + "</name><connectionParameters><url>" + fileUrl + "</url></connectionParameters></dataStore>";
            string str = "";
            try
            {
                str = CreatePost(url, xmlString, Encoding.UTF8);
                if (str == storeName)
                    return "数据源创建成功";
                else
                    return "数据源创建成功";
            }
            catch (Exception ex)
            {
                return "创建数据源失败:"+ex.Message;
            }
        }

        /// <summary>
        /// 在geoserver中发布数据源为图层
        /// </summary>
        /// <param name="workspaceName"></param>
        /// <param name="storeName"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        private static string PublishStore(string workspaceName, string storeName, string contentType)
        {
            string url = geoserver + "/rest/workspaces/" + workspaceName + "/datastores/" + storeName + "/featuretypes";
            string param = "<featureType><name>TestPoint</name></featureType>";
            try
            {
                string str = CreatePost(url, param, Encoding.UTF8);
                return "图层发布成功";
            }
            catch (Exception ex)
            {
                return "图层发布失败:"+ex.Message;
            }
        }

        /// <summary>
        /// 创建Http POST连接
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <param name="coding"></param>
        /// <returns></returns>
        private static string CreatePost(string url, string param, Encoding coding)
        {
            HttpWebRequest pRequest = null;
            HttpWebResponse pResponse = null;
            pRequest = WebRequest.Create(url) as HttpWebRequest;
            pRequest.Method = "POST";
            pRequest.KeepAlive = false;
            pRequest.ContentType = contentType;
            CredentialCache pCredential = new CredentialCache();
            pCredential.Add(new Uri(url), "Basic", new NetworkCredential(userName, password));
            pRequest.Credentials = pCredential;

            byte[] data = Encoding.ASCII.GetBytes(System.Text.RegularExpressions.Regex.Replace(param, "&$", ""));
            using (Stream stream = pRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            // 接收返回的消息
            pResponse = pRequest.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = pResponse.GetResponseStream();
            System.IO.StreamReader pReader = new System.IO.StreamReader(responseStream, coding);
            string str = pReader.ReadToEnd();
            return str;
        }

 

posted @ 2021-10-30 21:12  偶然路过的靓仔  阅读(460)  评论(0)    收藏  举报