来自shapefile

时间:2018-02-02 18:16:46

标签: python shapely geopandas fiona

我是处理空间数据的新手(如果我没有正确使用术语,请提前道歉)。我有一个形状文件,我与另一个csv文件合并,以从csv获取mea的名称。我是使用geopandas做到的。

msa_csv如下所示

  GEOID msa_code sql_code
 0  10580   albany        NaN
 1  10740   albuqu   albuquer
 2  12060   atlant    atlanta
 3  12420   austin     austin
 4  12580   baltim   baltimor

我正在使用下面的shapefile,我在下面调用cbsa_sp https://catalog.data.gov/dataset/tiger-line-shapefile-2015-nation-u-s-current-metropolitan-statistical-area-micropolitan-statist

msa_csv = gpd.GeoDataFrame(pd.read_csv("msa.csv"))
cbsa_sp = gpd.read_file('tl_2015_us_cbsa.shp') 
cbsa["GEOID"] = cbsa["GEOID"].astype(int)
new_cbsa = pd.merge( cbsa,msa_csv, on="GEOID")


new_cbsa

当我将cbsa shapefile与csv(或new_cbsa.head(5)合并时,我得到以下内容:

CSAFP   CBSAFP  GEOID   NAME    NAMELSAD    LSAD    MEMI    MTFCC   ALAND   AWATER  INTPTLAT    INTPTLON    geometry    FIPS    msa_code    sql_code
0   348 40140   40140   Riverside-San Bernardino-Ontario, CA    Riverside-San Bernardino-Ontario, CA Metro Area M1  1   G3110   70612017093 374752982   +34.5375344 -116.1309212    POLYGON ((-117.673749 33.870831, -117.673941 3...   40140   rivers  riversid
1   488 41860   41860   San Francisco-Oakland-Hayward, CA   San Francisco-Oakland-Hayward, CA Metro Area    M1  1   G3110   6417203879  2456442138  +37.7737185 -122.2744317    (POLYGON ((-122.612285 37.815224, -122.626091 ...   41860   san-fr  sanfranc
2   500 42660   42660   Seattle-Tacoma-Bellevue, WA Seattle-Tacoma-Bellevue, WA Metro Area  M1  1   G3110   15209110951 1132535454  +47.5530503 -121.8831653    POLYGON ((-122.438044 47.778118, -122.442245 4...   42660   seattl  seattle

请注意,在上面的csv文件中,我们在sql中的表将不会包含所有cbsa的mea' s。因此,当我加入两个文件来创建new_cbsa时,它只包含我们在sql server中的msa的多边形。我想向我们的服务器写一个查询,以获取new_cbsa中找到的msa_cvs中包含的每个msa的所有网格。我被告知我必须使用边界或使用wkt才能做到这一点?

query = """
             SELECT grid, census 
             FROM table 
             WHERE layer = 'lay' 
            AND MATCH(shape, '{}') USING within;
          """.format(cbsa_boundary)
geoCursor.execute(query)
cbsaDf = pd.DataFrame(geoCursor.fetchall(), columns = ['grid', 'census'])

我真的不知道如何制作边界或获得几何的几何系列。我试着查找如何将new_cbsa['geometry']等地理数据转换为wkt格式或边界

cbsa_shape = shape(new_cbsa)
cbsa_boundary = box(*cbsa_shape.bounds).wkt

但我没有运气。如果有人能指出我正确的方向,我将不胜感激。

0 个答案:

没有答案