2016-10-11 25 views
1

我似乎无法直接将mssql空间字段导入到geopand。我可以使用Pymssql将正常的mssql表导入Pandas中,但没有办法将空间字段导入到几何图形中。我知道用于mssql的OGR驱动程序应该能够处理它,但是我没有足够的技巧来解决这个问题。 这是线和多边形的更多问题,因为点可以从mssql字段转换为x和y坐标。 谢谢!将mssql空间字段导入到geopandas /几何形状

回答

1

我想通过正确查询sql数据库表并通过shapely.wkt中的加载函数将wkt字符串转换为几何形状几何图形。

我不是程序员,所以请牢记这个功能的组织。该函数可以导入具有或不具有GIS几何图形的mssql表格。

from pymssql import connect 
from pandas import read_sql 
from shapely.wkt import loads 
from geopandas import GeoDataFrame 

def rd_sql(server, database, table, col_names=None, where_col=None, where_val=None, geo_col=False, epsg=2193, export=False, path='save.csv'): 
    """ 
    Imports data from MSSQL database, returns GeoDataFrame. Specific columns can be selected and specific queries within columns can be selected. Requires the pymssql package, which must be separately installed. 
    Arguments: 
    server -- The server name (str). e.g.: 'SQL2012PROD03' 
    database -- The specific database within the server (str). e.g.: 'LowFlows' 
    table -- The specific table within the database (str). e.g.: 'LowFlowSiteRestrictionDaily' 
    col_names -- The column names that should be retrieved (list). e.g.: ['SiteID', 'BandNo', 'RecordNo'] 
    where_col -- The sql statement related to a specific column for selection (must be formated according to the example). e.g.: 'SnapshotType' 
    where_val -- The WHERE query values for the where_col (list). e.g. ['value1', 'value2'] 
    geo_col -- Is there a geometry column in the table? 
    epsg -- The coordinate system (int) 
    export -- Should the data be exported 
    path -- The path and csv name for the export if 'export' is True (str) 
    """ 
    if col_names is None and where_col is None: 
     stmt1 = 'SELECT * FROM ' + table 
    elif where_col is None: 
     stmt1 = 'SELECT ' + str(col_names).replace('\'', '"')[1:-1] + ' FROM ' + table 
    else: 
     stmt1 = 'SELECT ' + str(col_names).replace('\'', '"')[1:-1] + ' FROM ' + table + ' WHERE ' + str([where_col]).replace('\'', '"')[1:-1] + ' IN (' + str(where_val)[1:-1] + ')' 
    conn = connect(server, database=database) 
    df = read_sql(stmt1, conn) 

    ## Read in geometry if required 
    if geo_col: 
     geo_col_stmt = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=" + "\'" + table + "\'" + " AND DATA_TYPE='geometry'" 
     geo_col = str(read_sql(geo_col_stmt, conn).iloc[0,0]) 
     if where_col is None: 
      stmt2 = 'SELECT ' + geo_col + '.STGeometryN(1).ToString()' + ' FROM ' + table 
     else: 
      stmt2 = 'SELECT ' + geo_col + '.STGeometryN(1).ToString()' + ' FROM ' + table + ' WHERE ' + str([where_col]).replace('\'', '"')[1:-1] + ' IN (' + str(where_val)[1:-1] + ')' 
     df2 = read_sql(stmt2, conn) 
     df2.columns = ['geometry'] 
     geometry = [loads(x) for x in df2.geometry] 
     df = GeoDataFrame(df, geometry=geometry, crs={'init' :'epsg:' + str(epsg)}) 

    if export: 
     df.to_csv(path, index=False) 

    conn.close() 
    return(df) 

编辑:使功能自动找到几何字段,如果存在。