# 2.4.地理数据库

## 前言

shapefile无疑是简单方便的，但是其具有大小限制：2GB，尽管对于大多数数据集来说，数据量是无法达到2GB的。地理数据库的出现，则解决了这一缺点。ArcGIS 地理数据库是存储在通用文件系统文件夹、Microsoft Access 数据库或多用户关系 DBMS（如 Oracle、Microsoft SQL Server、PostgreSQL、Informix 或 IBM DB2）中的各种类型地理数据集的集合。

ArcGIS的个人地理数据库使用的是Access 数据库，而文件地理数据库则使用的是本地文件系统，这是我们最常用的两种地理数据库，下面我们来看下ArcPy是如何对两者进行数据访问的。

## 文件地理数据库

前面有讲到，文件地理数据库则使用的是本地文件系统，如果你打开xxx.gdb文件地理数据库所在目录，可以发现xxx.gdb就是一个文件夹，里面有许多命名无意义的文件，这些文件就是文件地理数据库的数据及元数据。对文件地理数据库中的要素类进行访问，与之前方法相同，只是要使用地理数据库中的要素类的名字，而不是shapefile的文件名了。例如读取：

```
fileGDB_dir = '../data/fileGDB.gdb'
feature_set_name = "point"
feature_set_path = fileGDB_dir + "/" + feature_set_name
with arcpy.da.SearchCursor(feature_set_path, ['Shape@XY', 'OID@']) as cursor:
    for row in cursor:
        for data in row:
            print(data),
        print('\n')
```

## 个人地理数据库

个人地理数据库使用的Access数据库，打开xxx.mdb所在的目录，双击xxx.mdb，你将可以用Access打开查看其数据（前提是你装了Microsoft Office Access组件）。对个人地理数据库中的要素类进行访问，与之前方法相同，你只要将xxx.mdb也当作是一个目录即可，例如：

```
personalGDB_fir = "../data/personalGDB.mdb/"
feature_set_name = "point"
    feature_set_path = personalGDB_fir + feature_set_name
    with arcpy.da.SearchCursor(feature_set_path, ['Shape@XY', 'OID@']) as cursor:
        for row in cursor:
            for data in row:
                print(data),
            print('\n')
```

ArcPy会对对于新建要素类的目录进行解析，如果目录是普通文件夹，就按照普通文件的操作进行数据访问；如果目录是xxx.gdb，则会判断是否为文件地理数据库，如果是地理数据库则按照地理数据库的方式进行数据访问，否则如果能够按照普通文件的操作进行数据访问则按照普通文件的操作进行数据访问，再否则，抛出异常；如果目录是xxx.mdb，则与文件地理数据库访问方式相同。（**注**：此为本人猜测，未曾找到相关证明）


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zxyao.gitbook.io/arcpy/2.4.-di-li-shu-ju-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
