数据库 映射类被映射类

    科技2026-09-03  7

    数据库 映射类被映射类

    Today I’m taking a look at the racial composition of Seattle, according to the 2010 Census. Towards this end, I’ll use Integrated Public Use Microdata Series (IPUMS) National Historical Geographic Information System (NHGIS). You can also use data.census.gov, which I found to be much slower (so much pinwheeling!) than the IPUMS-NHGIS system.

    根据2010年人口普查,今天我来看看西雅图的种族构成。 为此,我将使用综合公共用途微数据系列( IPUMS)国家历史地理信息系统(NHGIS)。 您还可以使用data.census.gov ,我发现它比IPUMS-NHGIS系统要慢得多(风车太多!)。

    Note: scroll to the bottom for a glossary of terms.

    注意:滚动至底部,查看术语表。

    获取人口普查数据 (Get census data)

    In order to map census data, we’re looking for both the GIS shapefiles at the level of interest, as well as the information tables at the matching levels.

    为了映射人口普查数据,我们正在寻找感兴趣级别的GIS shapefile,以及匹配级别的信息表。

    From IPUMS:

    从IPUMS:

    “IPUMS provides census and survey data from around the world integrated across time and space. IPUMS integration and documentation makes it easy to study change, conduct comparative research, merge information across data types, and analyze individuals within family and community contexts. Data and services available free of charge.”

    “ IPUMS提供了跨越时空整合的全球人口普查和调查数据。 IPUMS的集成和文档使研究变化,进行比较研究,合并跨数据类型的信息以及分析家庭和社区环境中的个人变得容易。 免费提供数据和服务。”

    Check out all the databases here. Or the GIS and census database here. To use this system, you’ll need to pick your data sets, sign up for an account, and wait for the system to email you that your data sets are ready (approx. 15 min wait for me).

    在此处签出所有数据库。 或这里的GIS和人口普查数据库。 要使用此系统,您需要选择数据集,注册一个帐户,然后等待系统通过电子邮件向您发送电子邮件,告知您数据集已准备就绪(大约等15分钟)。

    挑选数据集 (Picking data sets)

    Click on the (currently) green boxes to filter based on Geographic Areas, Years, Topics, Data Sets.

    单击(当前)绿色框以根据地理区域,年份,主题,数据集进行过滤。

    In the Geographic Areas pop up, you’ll need to know how the Census does geography. For areas that are not currently classified as “American Indian, Alaska Native, Native Hawaiian Areas,” you can read the Standard Hierarchy from top to bottom, cutting the nation into regions, each region into divisions, each division into states, etc. Alternatively, you can read the Standard Hierarchy from bottom to top: Census Blocks are grouped into Block Groups, which are grouped into Census Tracts, which are grouped into Counties, etc.

    在“地理区域”弹出窗口中,您需要知道人口普查如何进行地理处理。 对于当前未归类为“美洲印第安人,阿拉斯加原住民,夏威夷原住民地区”的地区,您可以从上至下阅读标准等级结构,将国家分为多个区域,每个区域划分为多个分区,每个区域划分为各州,等等。 ,您可以从下至上阅读“标准层次结构”:将人口普查块分组为块组,将其分组为人口普查区域,再将其分组为县,等等。

    https://www2.census.gov/geo/pdfs/reference/geodiagram.pdf?# https://www2.census.gov/geo/pdfs/reference/geodiagram.pdf?#

    One thing to note is that Census Tracts, Block Groups, and Census Blocks do not follow any of the divisions outside of the center vertical (state, county). In Census-speak, cities are “Places” whose boundaries are not necessarily accounted for in the creation of Census Blocks, Block Groups, or Tracts. You can get the data aggregated on these alternate levels/categories as well, but you’ll want to take care when attempting to “roll up” this data into larger groupings or “drill down” into smaller scales.

    需要注意的一件事是,人口普查区,街区组和人口普查区不遵循中心垂直线(州,县)以外的任何划分。 用人口普查来说,城市是“地点”,在创建人口普查街区,街区组或地区时,其边界不一定要考虑在内。 您也可以在这些备用级别/类别上汇总数据,但是在尝试将这些数据“汇总”为较大的组或“向下追溯”为较小的比例时,需要小心。

    Once you have your filters set, click the tabs under “Select data” to move from “Source Tables” to “Time Series Tables” or “GIS Files.” You’ll narrow down to your state, county, etc. in the next step.

    设置好过滤器后,单击“选择数据”下的选项卡,从“源表”移至“时间序列表”或“ GIS文件”。 下一步,您将缩小范围到州,县等。

    Once you’ve selected your data of interest, click continue from your “data cart” at the top right of your screen.

    选择感兴趣的数据后,从屏幕右上方的“数据购物车”中单击继续。

    选择数据选项 (Select data options)

    Depending on your data set and geographic level, you may be able to narrow down the data by state on this page. If you are using county-level data, this state-level filtering is not an option.

    根据您的数据集和地理级别,您可以在此页面上按州缩小数据范围。 如果使用县级数据,则不能选择此州级过滤。

    You’re ready to “continue” on to review your data cart.

    您准备“继续”继续查看数据购物车。

    申请帐号 (Signing up for an account)

    Photo by Avel Chuklanov on Unsplash Avel Chuklanov在 Unsplash上的 照片

    Pretty standard here, although there are a lot of required fields.

    尽管有很多必填字段,但在这里很标准。

    If you are not using the data in a professional capacity, you can use “Personal/Non-Professional Use” for Name of Institution or Employer, or “Self-Employed/Consultant” if that is a better fit.

    如果您不是以专业身份使用数据,则可以使用“个人/非专业用途”作为机构或雇主的名称,或者使用“自雇/顾问”(如果合适)。

    The most important things to note are the terms of use:

    需要注意的最重要的事情是使用条款:

    “Redistribution: You will not redistribute the data without permission.”

    “重新分发:未经许可,您将不会重新分发数据。”

    “Citation: Cite the NHGIS data appropriately.”

    “引文:适当引用NHGIS数据。”

    等待您的数据电子邮件 (Wait for your data email)

    It will be from nhgis@umn.edu and have link to follow, where you can download, revise, or resubmit your data request.

    它将来自nhgis@umn.edu,并具有链接,您可以在此处下载,修改或重新提交数据请求。

    备用shapefile来源:census.gov (Alternative shapefile source: census.gov)

    Although the data tables functionality of data.census.gov seems to struggle (at the time of writing), I found the shapefile (aka Cartographic Boundary File) page to be much more user-friendly. A little quirk: here’s the page for 1990–2018, and there is a separate 2019 page. The newly released 2019 data conveniently includes a range of scale options.

    尽管data.census.gov的数据表功能似乎很困难(在撰写本文时),但我发现shapefile(又称“制图边界文件”)页面更加用户友好。 有点奇怪:这是1990- 2018年的页面,另外还有2019年的页面。 新发布的2019年数据方便地包括一系列比例尺选项。

    When you use the dropdown menu to make a selection, it automatically starts the download. Maybe not the most intuitive, but at least they’re zipped. So any mis-clicks don’t have too large of an impact.

    使用下拉菜单进行选择时,它将自动开始下载。 也许不是最直观的,但至少它们是压缩的。 因此,任何误点击都不会产生太大影响。

    了解数据 (Understanding the data)

    Each table file from IPUMS comes with a “codebook” text file containing a Data Summary, Data Dictionary, and Citation and Use reminder. You can learn more about the specific data, classifications and questions of interest from the Census Bureau.

    IPUMS的每个表文件都带有一个“密码本”文本文件,其中包含数据摘要,数据字典以及“引用和使用”提醒。 您可以从人口普查局了解有关特定数据,分类和感兴趣的问题的更多信息。

    Note that these shapefiles are generalized, bringing pros and cons (courtesy the Census Bureau): Pros — appearance at small scale, disk space, display time. Cons: imperfect area and perimeter representation, inaccurate address geocoding, some excluded areas, not always aligned across years.

    请注意,这些shapefile是通用的,具有优点和缺点(由人口普查局提供):优点-小规模外观,磁盘空间,显示时间。 缺点:不完善的区域和边界表示,不正确的地址地理编码,某些排除的区域,并非总是跨年对齐。

    I need to shout out to the Census Bureau’s Geography Program. They are a wealth of intuitive and helpful resources.

    我需要大声疾呼人口普查局的地理计划。 它们是大量直观且有用的资源。

    绘制数据图 (Graphing the data)

    在熊猫和大熊猫中加载数据(Loading data in pandas and geopandas)

    For some files, I had no problems with pd.read_csv(), but on one, I got UnicodeDecodeError. I’m not sure if it’s the file (perhaps an excel issue?) or a character used, but I found setting the encoding to latin1, worked for me:

    对于某些文件, pd.read_csv()没有任何问题,但其中一个出现了UnicodeDecodeError 。 我不确定使用的是文件(也许是excel问题?)还是使用的字符,但是我发现将编码设置为latin1对我有用:

    counties = pd.read_csv('counties.csv', encoding='latin-1')

    GeoPandas used to be tricky to install, but it seems like the newest version has solved a lot (all?) of these issues. Still, it might make sense for you use a new or geopandas-focused environment. You can find their install recommendations here. Today I’ll be using geopandas for plotting/mapping, so I’ll also install matplotlib and descartes in my new environment. I haven’t tried out the experimental (at time of writing) use of PyGEOS, but it seems like it could be awesome, especially if you are looking for speed.

    GeoPandas过去安装起来很棘手,但最新版本似乎解决了很多(全部?)问题。 不过,对于您而言,使用新的或以geopandas重点的环境可能仍然有意义。 您可以在此处找到其安装建议。 今天,我将使用geopandas进行绘制/映射,因此我还将在新环境中安装matplotlib和descartes 。 我还没有尝试过PyGEOS的实验性使用(在撰写本文时),但是它看起来可能很棒,尤其是在您追求速度的情况下。

    缩小目标城市 (Narrowing to target city)

    My downloaded data set contained all the census blocks in Washington State. All 1.96 million of them.

    我下载的数据集包含华盛顿州的所有人口普查区块。 全部196万。

    At first, I planned on using the GeoPandas / Shapely intersects function along with the Seattle geography from the place map. But once I saw that the places geo-data set includes the place name, I decided to use the numeric place code data to filter.

    首先,我计划将GeoPandas / Shapely相交函数与位置地图中的Seattle地理一起使用。 但是,一旦我看到地点地理数据集包含地点名称,便决定使用数字地点代码数据进行过滤。

    Alternatively, you can search census.gov for [year] FIPS Codes, download the appropriate file (in my case the “State, County, Minor Civil Division, and Incorporated Place FIPS Codes”), and do a quick search for your location.

    或者,您可以在census.gov上搜索[年份] FIPS代码,下载适当的文件(在我的情况下为“州,县,小型民政部门和公司所在地的FIPS代码”),然后快速搜索您的位置。

    Both strategies involve an extra download.

    两种策略都需要额外下载。

    一言以蔽之 (A word on map projections)

    Map of Seattle Census Blocks turned ~15 degrees clockwise 西雅图人口普查区地图顺时针旋转约15度

    I noticed that the Census Block Shapefile is set to a different projection that the Census Places Shapes. You might choose to reorient your map to match the Census Place shape, the other data you’re using (eg. from the city), or not. In this case, I’ll reorient to the view I’m more accustomed to.

    我注意到,“人口普查区块形状”文件设置为与“人口普查放置形状”不同的投影。 您可以选择调整地图的方向以匹配“人口普查”地点的形状,正在使用的其他数据(例如,来自城市的数据),也可以不匹配。 在这种情况下,我将重新定位我更习惯的视图。

    You can check the orientation / crs of a file, use geo_object.crs or use the convenient warning message you’ll get if you use the Shapely geometric comparisons (ie. contains, touches, etc.)

    您可以检查文件的方向/ crs,使用geo_object .crs或使用便捷的警告消息(如果您使用Shapely几何比较(即包含,触摸等))。

    To set the projection: seattle_blocks_map.to_crs(“EPSG:4269”)

    设置投影: seattle_blocks_map.to_crs(“EPSG:4269”)

    根据功能为地图着色 (Coloring the map based on a feature)

    For a basic choropleth, add a column argument to your plot call. You can read more about choropleths on GeoPandas. Or check out my code to create the map with a legend, and grey background to fill in any gaps:

    对于基本的choropleth,请将列参数添加到绘图调用中。 您可以在GeoPandas上阅读有关节律的更多信息。 或查看我的代码以创建带有图例和灰色背景的地图,以填补所有空白:

    # to make nice looking legendsfrom mpl_toolkits.axes_grid1 import make_axes_locatable# create the plot for sizingfig, ax = plt.subplots(figsize=(20,20))# lay down a background map for areas with no peopleseattle_blocks_map.plot(ax=ax, color='grey', alpha=.5)# set the legend specificationsdivider = make_axes_locatable(ax)cax = divider.append_axes("right", size="5%", pad=0.05)# add the map with the choroplethseattle_blocks_map.plot(ax=ax, column='white_rate', cmap='Purples_r', legend=True, cax=cax, missing_kwds={"color": "lightgrey"}, )

    I’m excited to dig deeper into this data, and to see how much has changed when the 2020 numbers come out!

    我很高兴能够更深入地研究这些数据,并看到2020年的数字出现了多大变化!

    通用,可重用的解决方案 (Generic, reusable solution)

    After mapping for Seattle, I was curious about other cities, and decided to make a more generic solution. To do this, I created a function that takes the data file paths, and does the same as above, allowing me to effortlessly make similar maps for the four next largest cities in Washington. I’ll have to report back on cities in other states after another round of data downloads.

    在为西雅图绘制地图之后,我对其他城市感到好奇,并决定做出一个更通用的解决方案。 为此,我创建了一个使用数据文件路径的函数,并且与上述功能相同,从而使我能够毫不费力地为华盛顿的下四个最大城市绘制相似的地图。 在另一轮数据下载之后,我将不得不报告其他州的城市。

    You can find the jupyter notebook here. Happy Coding!

    您可以在这里找到jupyter笔记本。 编码愉快!

    Photo by William Montout on Unsplash William Montout在 Unsplash上 拍摄的照片

    词汇表:(Glossary:)

    Place: in the Census data, place means city/town/etc.

    地点:在人口普查数据中,地点是指城市/城镇/等。

    crs: coordinate reference system. The projection of your (spherical) geometric shapes onto a two-dimensional plane.

    crs :坐标参考系。 您的(球形)几何形状在二维平面上的投影。

    shapefile: a set of files that contain geometries and the other information in a geo-dataset. Learn more in this post.

    shapefile :一组文件,这些文件包含地理数据集中的几何形状和其他信息。 在这篇文章中了解更多信息。

    引文: (Citations:)

    IPUMS NHGIS, University of Minnesota, www.nhgis.org

    明尼苏达大学IPUMS NHGIS, www.nhgis.org

    Cartographic Boundary File Description. US Census Bureau. https://www.census.gov/programs-surveys/geography/technical-documentation/naming-convention/cartographic-boundary-file.html

    制图边界文件说明。 美国人口普查局。 https://www.census.gov/programs-surveys/geography/technical-documentation/naming-convention/cartographic-boundary-file.html

    翻译自: https://towardsdatascience.com/mapping-census-data-fbab6722def0

    数据库 映射类被映射类

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.013, SQL: 9