Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion docs/tutorial/files/geoparquet-sedona-spark.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,44 @@ df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")

Sedona's functions, such as `ST_AsText` and `ST_Buffer`, accept such a column. Spark's built-in functions, such as `ST_SRID` and `ST_AsBinary` on Spark 4.2, accept native geometry columns only.

`GEOGRAPHY` columns are read as binary columns that hold WKB, also when the GeoParquet metadata lists them. Convert them with `ST_GeogFromWKB`.
`GEOGRAPHY` columns are read as geography columns: Sedona's geography type on Spark 4.1, and Spark's native geography type on Spark 4.2 and later. The SRID comes from the `crs` property by the rules above, so a column without one has SRID 4326, and a column in another geographic CRS, such as NAD83 (`EPSG:4269`), has the SRID of that CRS.

Three kinds of `GEOGRAPHY` columns need a schema from you.

The first is a column whose edges follow an algorithm other than `spherical`, which is `vincenty`, `thomas`, `andoyer` or `karney`. Sedona computes on a sphere, so it rejects the column. A schema in which the column is binary returns its WKB:

```python
from pyspark.sql.types import BinaryType, StructField, StructType

schema = StructType([StructField("geography", BinaryType())])
df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")
```

The second, on Spark 4.2 and later, is a column whose CRS is unknown or not geographic. Spark's native geography type cannot represent it. A schema with Sedona's own geography type, `LegacyGeographyType` in Python and `GeographyUDT` in Scala and Java, reads the column:

```python
from pyspark.sql.types import StructField, StructType
from sedona.spark.sql.types import LegacyGeographyType

schema = StructType([StructField("geography", LegacyGeographyType())])
df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")
```

The third, on Spark 4.2 and later, is a column for which the files state different SRIDs. Spark's native geography type names one SRID for a column, so Sedona rejects the read before it scans the files. A schema with the type `GEOGRAPHY(ANY)` for the column reads them, and each geography keeps the SRID of its file:

```python
df = (
sedona.read.schema("geography GEOGRAPHY(ANY)")
.format("geoparquet")
.load("/tmp/somewhere")
)
```

A `GEOGRAPHY` column is not read as a geometry column. Sedona rejects a schema that gives it a geometry type, and it rejects files in which a column is a geometry column in some and a `GEOGRAPHY` column in others. This holds for a geometry column of a GeoParquet 1.x file as well as for a `GEOMETRY` column.

A file that Sedona rejects for one of these reasons is not a corrupt file. The read fails for it with the option `ignoreCorruptFiles` as well, and does not leave the file out.

Sedona applies a filter on a geography column to the rows and does not use the bounding boxes of the files for it, because these are planar.

Spark 3.5 and 4.0 ship a Parquet library that predates these logical types and cannot open such files.

Expand Down
39 changes: 38 additions & 1 deletion docs/tutorial/files/geoparquet-sedona-spark.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,44 @@ df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")

Sedona 的函数,例如 `ST_AsText` 和 `ST_Buffer`,可以处理这样的列。Spark 内置的函数,例如 Spark 4.2 上的 `ST_SRID` 和 `ST_AsBinary`,只接受原生几何列。

`GEOGRAPHY` 列会被读取为保存 WKB 的二进制列,即使 GeoParquet 元数据列出了它们也是如此,可以用 `ST_GeogFromWKB` 转换。
`GEOGRAPHY` 列会被读取为地理列:在 Spark 4.1 上是 Sedona 的地理类型,在 Spark 4.2 及更高版本上是 Spark 原生地理类型。SRID 按上述规则取自 `crs` 属性,因此没有该属性的列 SRID 为 4326,使用其他地理坐标系(例如 NAD83,即 `EPSG:4269`)的列 SRID 为该坐标系的 SRID。

有三类 `GEOGRAPHY` 列需要您提供 schema。

第一类是边的插值算法不是 `spherical` 的列,即 `vincenty`、`thomas`、`andoyer` 或 `karney`。Sedona 在球面上计算,因此会拒绝这样的列。在 schema 中把该列声明为二进制类型,即可读出它的 WKB:

```python
from pyspark.sql.types import BinaryType, StructField, StructType

schema = StructType([StructField("geography", BinaryType())])
df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")
```

第二类是在 Spark 4.2 及更高版本上,CRS 未知或不是地理坐标系的列。Spark 原生地理类型无法表示这样的 CRS。在 schema 中为该列使用 Sedona 自己的地理类型即可读取,在 Python 中是 `LegacyGeographyType`,在 Scala 和 Java 中是 `GeographyUDT`:

```python
from pyspark.sql.types import StructField, StructType
from sedona.spark.sql.types import LegacyGeographyType

schema = StructType([StructField("geography", LegacyGeographyType())])
df = sedona.read.schema(schema).format("geoparquet").load("/tmp/somewhere")
```

第三类是在 Spark 4.2 及更高版本上,各文件声明的 SRID 不一致的列。Spark 原生地理类型的一列只有一个 SRID,因此 Sedona 会在扫描文件之前拒绝读取。在 schema 中为该列使用 `GEOGRAPHY(ANY)` 类型即可读取这些文件,每个地理对象保留其所在文件的 SRID:

```python
df = (
sedona.read.schema("geography GEOGRAPHY(ANY)")
.format("geoparquet")
.load("/tmp/somewhere")
)
```

`GEOGRAPHY` 列不会被读取为几何列。如果 schema 为它指定几何类型,Sedona 会拒绝读取;如果同一列在一部分文件中是几何列、在另一部分文件中是 `GEOGRAPHY` 列,Sedona 也会拒绝读取。这既适用于 `GEOMETRY` 列,也适用于 GeoParquet 1.x 文件中的几何列。

因上述原因被 Sedona 拒绝的文件并不是损坏的文件。即使设置了 `ignoreCorruptFiles` 选项,读取也会失败,而不会跳过该文件。

对地理列的过滤条件,Sedona 会逐行计算,不会使用文件的边界框,因为这些边界框是平面的。

Spark 3.5 和 4.0 自带的 Parquet 库早于这些逻辑类型,无法打开此类文件。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ object SpatialTypeSupport {
case native: GeometryType if !native.isMixedSrid => Some(native.srid)
case _ => None
}
def geographySrid(dataType: DataType): Option[Int] = dataType match {
case native: GeographyType if !native.isMixedSrid => Some(native.srid)
case _ => None
}

/** Spark's native geography type takes the SRID of a geographic CRS only. */
def supportsGeographySrid(srid: Int): Boolean = GeographyType.isSridSupported(srid)

/** The value of a geography column for the geography that `wkb` is the WKB of. */
def geographyFromWkb(wkb: Array[Byte], srid: Int, dataType: DataType): Any = dataType match {
case _: GeographyUDT => GeographySerializer.serializeWkb(wkb, srid)
case native: GeographyType =>
native.assertSridAllowedForType(srid)
NativeGeography.fromWkb(PublicWkbHeaderAdapter.toIsoWkb(wkb), srid).getValue
case other => throw new IllegalArgumentException(s"Expected a geography type, got $other")
}

/**
* The SRID that Spark's registry of coordinate reference systems gives to `crs`, an identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.nio.ByteOrder

import org.apache.sedona.common.S2Geography.Geography
import org.apache.sedona.common.geography.Constructors
import org.apache.sedona.sql.utils.GeographySerializer
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder
import org.apache.spark.sql.catalyst.expressions.{Expression, SpecializedGetters}
import org.apache.spark.sql.sedona_sql.UDT.{GeographyUDT, GeometryUDT}
Expand All @@ -35,6 +36,16 @@ object SpatialTypeSupport {
def unwrapGeographyInput(expression: Expression): Expression = expression
def geometryType: DataType = GeometryUDT()
def geometryType(srid: Int): DataType = geometryType
def geographyType: DataType = GeographyUDT()
def geographyType(srid: Int): DataType = geographyType
def geographySrid(dataType: DataType): Option[Int] = None

/** Sedona's own geography type takes any SRID. */
def supportsGeographySrid(srid: Int): Boolean = true

/** The value of a geography column for the geography that `wkb` is the WKB of. */
def geographyFromWkb(wkb: Array[Byte], srid: Int, dataType: DataType): Any =
GeographySerializer.serializeWkb(wkb, srid)
def geometrySrid(dataType: DataType): Option[Int] = None

/** Spark has a registry of coordinate reference systems only where it has native types. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ object GeographySerializer {
case value: WKBGeography => value.getWKBBytes
case _ => new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN, false).write(geography)
}
serializeWkb(source, geography.getSRID)
}

/**
* The SQL transport of the geography that `source` is the WKB of. The coordinates are not
* parsed, so a geography that is only passed on costs no JTS or S2 object.
*/
def serializeWkb(source: Array[Byte], srid: Int): Array[Byte] = {
val wkb = PublicWkbHeaderAdapter.toIsoWkb(source)
ByteBuffer
.allocate(Integer.BYTES + wkb.length)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(geography.getSRID)
.putInt(srid)
.put(wkb)
.array()
}
Expand Down
Loading
Loading