PostgreSQL & PostGis 操作






4.08/5 (8投票s)
在本文中,我想向您展示如何(备份、恢复)数据库以及如何在 PostGIS 和 Esri shape 文件之间进行转换
介绍
在本文中,我想展示一些 PostgreSQL 和 PostGIS 的操作
- PostgreSQL:创建数据库的备份并将备份文件恢复到您的数据库
- PostGIS:从 PostGIS 创建 shape 文件并将 shape 文件转换为 PostGIS
背景
我假设您对 PostgreSQL 有基本的了解。如果您需要帮助,请阅读
- 在您的 C# (.NET) 应用程序中使用 PostgreSQL(简介),作者:Adrian basic
PostGIS 是 PostgreSQL 对象关系数据库系统的扩展,它允许将 GIS(地理信息系统)对象存储在数据库中。PostGIS 对 PostgreSQL 所做的事情就像 Oracle Spatial 对 Oracle,ArcSDE 对 Microsoft SQL Server/Oracle。
我假设您已经在 (Driver C) 中安装了 postgreSQL(版本 8.3),并且还安装了 PostGIS(版本 1.3.6)。
请检查以下路径 C:\\Program Files\\PostgreSQL\\8.3\\bin 包含这些可执行文件 (pg_dump.exe, pg_restore.exe, shp2pgsql.exe, pgsql2shp.exe)。
Using the Code
连接到 PostgreSQL
Npgsql.NpgsqlConnection npgConnection = null;
npgConnection = new NpgsqlConnection("Server="+hostName+";UID=" +
userName + ";PWD=" + password + ";Database=" + txtDB.Text + ";Port=5432;");
我使用 Npgsql
库来创建到 postgreSQL 的连接。
执行命令
我创建了一个名为 executeCommand
的方法,以发挥命令提示符的相同作用
private void executeCommand(string commandType,string commandSentence )
{
try
{
System.Diagnostics.ProcessStartInfo info =
new System.Diagnostics.ProcessStartInfo();
info.FileName = "C:\\Program Files\\PostgreSQL\\8.3\\bin\\" +
commandType + ".exe ";
info.Arguments = commandSentence;
info.CreateNoWindow = true;
info.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
if (commandType == "pg_dump")
toolStripStatusLabel1.Text = "Backup successfully created";
else if (commandType == "pg_restore")
toolStripStatusLabel1.Text = "Restore successfully executed";
else if(commandType=="shp2pgsql")
toolStripStatusLabel1.Text =
"Your selected shape file successfully transferred to PostGIS";
else if (commandType == "pgsql2shp")
toolStripStatusLabel1.Text = "Your selected layer from
PostGIS successfully converted to shape file";
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = ex.ToString();
}
}
我将两个参数 commandType
(告诉我 processStartInfo
的文件)和 commandSentence
(告诉我要执行的命令)传递给方法 executeCommand
。
备份您的数据库
在这里,我可以创建数据库的备份,以便轻松加载到另一台机器上:
string cmd = "-i -h "+txtHost.Text+" -p "+txtPort.Text+" -U "+txtUserName.Text+"
-F c -b -v -f " + tempPath + txtDB.Text + ".backup " + txtDB.Text;
executeCommand("pg_dump", cmd);
- -p, –port=PORT 数据库服务器端口号
- -i, –ignore-version 即使服务器版本不匹配也继续
- -h, –host=HOSTNAME 数据库服务器主机或套接字目录
- -U, –username=NAME 以指定的数据库用户身份连接
- -W, –password 强制密码提示(应该自动发生)
- -d, –dbname=NAME 连接到数据库名称
- -v, –verbose 详细模式
- -F, –format=c|t|p 输出文件格式(自定义、tar、纯文本)
- -c, –clean 在创建之前清理(删除)模式
- -b, –blobs 在转储中包含大对象
- -v, –verbose 详细模式
- -f, –file=FILENAME 输出文件名
然后我使用命令类型 pg_dump
调用 executeCommand()
方法,这使我能够创建备份,就像我使用 FolderBrowserDialog
选择的路径一样。
恢复您的数据库
在这里,我可以将备份文件加载到我的数据库中
string cmd = "-i -h "+txtHost.Text+" -p "+txtPort.Text+" -U
"+txtUserName.Text+" -d " + txtDB.Text + " -v " + path;
executeCommand("pg_restore", cmd);
- -p, –port=PORT 数据库服务器端口号
- -i, –ignore-version 即使服务器版本不匹配也继续
- -h, –host=HOSTNAME 数据库服务器主机或套接字目录
- -U, –username=NAME 以指定的数据库用户身份连接
- -d, –dbname=NAME 连接到数据库名称
- -v, –verbose 详细模式
然后我使用命令类型 pg_restore
调用 executeCommand()
方法,这使我能够将备份文件加载到 PostgreSQL 中。
Shape 到 PostGIS (Loader)
我可以将 shape 文件加载到我的空间数据库 (PostGIS) 中作为表(或图层),使我能够进行空间数据库的所有操作。然后,您可以检查此图层是否确实包含原始 shapefile 的所有数据,方法是使用简单的 GIS 桌面应用程序,如 Quantum GIS 或 MapWindow,它们都可以轻松地连接到 PostGIS。
string cmd = "–I –D "+path+" "+fileName[0]+" | psql "+txtDB.Text+" "+ userName;
executeCommand("shp2pgsql", cmd);
- -I: 这意味着创建空间索引
- –D: 使用转储格式加载(更快)?
- path: shape 文件的路径
- fileName[0]: 将文件加载到的表名。
- 管道 (|):将 shp2pgsql 的输出发送到 psql
- Psql: PostgreSQL 的命令行 sql 客户端
- txtDB.txt: 您要将 shape 文件加载到的数据库
PostGIS 到 Shape (Dumper)
我可以从您从 PostGIS 获得的可用图层列表中选择的指定图层创建 shape 文件。
string Cmd = " -u " + userName + " -P " + password + " " +
txtDB.Text + " " + layerName + " -f " + path + "\\" + layerName + ".shp";
executeCommand("pgsql2shp", Cmd);
- -h, –host=HOSTNAME 数据库服务器主机或套接字目录
- -u, –username=NAME 以指定的数据库用户身份连接
- -f, –file=FILENAME 输出文件名
然后,您可以轻松地在自定义应用程序或在其他地方使用此 shape 文件。
结束语
我的应用程序不是唯一的,但它为您提供了 PostgreSQL 和 PostGIS 都可以做的一些功能。
此外,我的应用程序假设所有操作都是通过命令提示符执行的。
我希望这个应用程序对所有读者都有帮助。
