免费视频|新人指南|投诉删帖|广告合作|地信网APP下载

查看: 1575|回复: 1
收起左侧

[经验共享] 使用ArcGIS读取 SQLite 中的空间数据

[复制链接]

109

主题

1993

铜板

2

好友

VIP会员

Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23

积分
1037
发表于 2017-6-26 14:26 | 显示全部楼层 |阅读模式
本帖最后由 miki888 于 2017-6-26 14:30 编辑

对于 SQLite 里面原生的空间数据,spatialite.dll(需要几个依赖库)这个库读取,这样的话,我们将创建好的数据在脱离 ArcGIS 软件后也可以方便读取,比如在 WP 中,读取出来的数据,我们可以解析成 ArcGIS Runtime for WP 中的数据,以便使用。 下面给出代码示例和截图,在示例用已经将空间数据读取出来了
string          conn = @"Data
Source=D:\2013\SQLite\spatialtest.sqlite;  Pooling=true;FailIfMissing=false" ;
SQLiteCommand       cmd;
SQLiteConnection    pConn = new SQLiteConnection( conn );

if ( pConn.State != ConnectionState.Open )
{
    pConn.Open();
}

cmd     = new SQLiteCommand( String.Format( "SELECT load_extension('{0}');", "spatialite.dll" ) );
cmd.Connection  = pConn;
cmd.ExecuteNonQuery(); /* 加载类库后必须执行这句,不然会出问题 */

/* 可以查看表名称 */
DataTable dt = pConn.GetSchema( "Tables" );
Console.WriteLine( "打印表名称......" );
foreach ( DataRow dataRow in dt.Rows )
{
    string tableName = dataRow["TABLE_NAME"].ToString();
    Console.WriteLine( tableName );
}
cmd.CommandType = CommandType.Text;
string commandText = "Select AsBinary(Shape),* from POI";
cmd.CommandText = commandText;
DataSet ds = new DataSet();
;
SQLiteDataAdapter da = new SQLiteDataAdapter( cmd );
da.Fill( ds ); 6
pConn.Close();
Console.WriteLine( "空间数据......" );

foreach ( DataRow dataRow in ds.Tables[0].Rows )
{
    string tableName = dataRow["AsText(Shape)"].ToString();
    Console.WriteLine( tableName );
}
Console.ReadLine();

使用ArcGIS读取 SQLite 中的空间数据

使用ArcGIS读取 SQLite 中的空间数据


image0623A037.png
从 SQLite 中读取了空间数据,那么可以根据需要迚行转换,下面是我写的一个 AO 中的点对象和 SQLite 中点对象的转换(在 ArcGIS 中这个已经不用做了,Esri 已经帮我们实现)。
public static String IGeometryToWKT( IGeometry pGeometry )
{
    if ( pGeometry == null )
    {
        return(null);
    }
    String sGeoStr = "";
    if ( pGeometry is IPoint )
    {
        Point pt = (Point) pGeometry;
        sGeoStr = "POINT" + "(" + pt.X + " " + pt.Y + ")";
    }else{ /* 其余格式,省略 */
        sGeoStr = null; 9
    }
    return(sGeoStr);
}


public static IGeometry WKTToIGeometry( String sWkt )
{
    IGeometry pGeo = null;
    if ( sWkt == null || sWkt == "" )
    {
        return(null);
    }
    String  headStr = sWkt.Substring( 0, sWkt.IndexOf( "(" ) );
    String  temp    = sWkt.Substring( sWkt.IndexOf( "(" ) + 1, sWkt.LastIndexOf( ")" ) );
    if ( headStr.Equals( "Point", StringComparison.InvariantCultureIgnoreCase ) )
    {
        String[] values = temp.Split( ' ' );
        pGeo = new PointClass
        {
            X   = Convert.ToDouble( values[0] ),
            Y   = Convert.ToDouble( values[1] )
        };
    }else{
        /* 其余的省略 */
    }
    return(pGeo);
}

0

主题

2万

铜板

15

好友

版主

Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15

积分
2420

宣传勋章爱心勋章组织勋章优秀斑主地信元老灌水勋章荣誉会员勋章活跃勋章官方团队地信专家组VIP勋章贡献勋章名人堂勋章成就学员勋章

发表于 2017-6-26 19:15 | 显示全部楼层
感谢楼主分享,很有技术性
回复 支持 反对

使用道具 举报

37

主题

2万

铜板

111

好友

钻石会员

Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26

积分
5776
发表于 2022-4-12 10:10 | 显示全部楼层
谢谢分享,难度比较大
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线客服
快速回复 返回顶部 返回列表