首页 > 解决方案 > 如何在 SQL Server 数据库中存储地理文件,例如 shapefile (.shp)?

问题描述

我的电脑中有一些扩展名为 (.shp) 的 shapefile。但我知道任何单个 shapefile 都由三个强制性文件组成,即 .shp、.shx 和 .dbf。这些文件使 shapefile 被识别为 shapefile。

我想在 Visual Studio 中编写一些代码,使我能够浏览计算机位置并找到此类文件,然后如果找到文件,我想将它们保存在 SQL Server 数据库中。

如何检查所选文件是否真的是 shapefile?我应该编写什么代码才能在 SQL Server 中使用这些地理数据?

我听说 SQL Server 支持地理数据。

链接:https ://docs.microsoft.com/en-us/sql/relational-databases/spatial/spatial-data-sql-server?view=sql-server-2017

我已经创建了 Windows 窗体应用程序和一个按钮来浏览文件。

代码如下所示

  Private Sub BtnBrowse_Click(sender As System.Object, e As 
   System.EventArgs) Handles BtnBrowse.Click
    OpenFileDialog1.InitialDirectory = "E:\"
    OpenFileDialog1.Filter = "shapefile files (*.shp)|*.|All files 
    (*.*)|*.*"
    OpenFileDialog1.ShowDialog()
    txtFileName.Text = OpenFileDialog1.FileName

  End Sub

  Private Sub BtnSave_Click(sender As System.Object, e As 
   System.EventArgs) Handles BtnSave.Click
    Try
        'code to check if it is a shapefile



        'code to save shapefile in database 

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    End Sub

我希望单击 BtnSave 时,代码应检查所选文件是否为 shapefile,如果为 true,则应将文件保存到 sql server,否则应抛出所选文件不是 shapefile 的异常。

标签: sql-servervb.netgisshapefilespatial-query

解决方案


shapefile的基本理解1

1) shapefile 的所有支持文件具有完全相同的名称和不同的扩展名。2).shp,.shx 和 .dbf 是强制性的,.prj,.sbn 和.sbx,.fbn 和 .fbx,.ain 等是附加文件

带有有效性检查代码的代码片段:

      Try
                if(abc.shp)
               { 
                if ((abc.shx) && (abc.dbf)) `This is the condition for valid shapefile`
 {
//code should fetch all the files with exactly same name and different possible extensions of supporting files. You can get the list in link mentioned below

  return valid_shapefile
        }

    }

推荐阅读