---
title: "GEOS API"
version: 6.0
locale: ja
source: https://docs.djangoproject.com/ja/6.0/ref/contrib/gis/geos/
canonical: https://djangodocs.dev/ja/6.0/ref/contrib/gis/geos/
---
# GEOS API

## 背景

### GEOS とは？

[GEOS](https://libgeos.org/) stands for **Geometry Engine - Open Source**,
and is a C++ library, ported from the  [Java Topology Suite](https://sourceforge.net/projects/jts-topo-suite/). GEOS
implements the OpenGIS [Simple Features for SQL](https://www.ogc.org/standard/sfs/) spatial predicate functions
and spatial operators. GEOS, now an OSGeo project, was initially developed and
maintained by [Refractions Research](http://www.refractions.net/) of Victoria, Canada.

### 特徴

GeoDjango は GEOS ライブラリの高レベルな Python ラッパーを実装しており、その機能には次のものがあります:

- `ctypes` を使用して純粋に Python で実装された GEOS ジオメトリルーチンへの BSD ライセンスのインターフェイス。
- Loosely-coupled to GeoDjango. For example, [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) objects
  may be used outside of a Django project/application. In other words,
  no need to have [`DJANGO_SETTINGS_MODULE`](/ja/6.0/topics/settings/#envvar-DJANGO_SETTINGS_MODULE) set or use a database, etc.
- **ミュータビリティ (Mutability):** [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトは変更可能です。
- クロスプラットフォームでテストされています。

## チュートリアル

このセクションには、 [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトの簡単な紹介とチュートリアルが含まれています。

### ジオメトリを作成する

[`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) objects may be created in a few ways. The first is
to simply instantiate the object on some spatial input -- the following
are examples of creating the same geometry from WKT, HEX, WKB, and GeoJSON:

```pycon
>>> from django.contrib.gis.geos import GEOSGeometry
>>> pnt = GEOSGeometry("POINT(5 23)")  # WKT
>>> pnt = GEOSGeometry("010100000000000000000014400000000000003740")  # HEX
>>> pnt = GEOSGeometry(
...     memoryview(
...         b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x007@"
...     )
... )  # WKB
>>> pnt = GEOSGeometry(
...     '{ "type": "Point", "coordinates": [ 5.000000, 23.000000 ] }'
... )  # GeoJSON
```

Another option is to use the constructor for the specific geometry type
that you wish to create. For example, a [`Point`](#django.contrib.gis.geos.Point) object may be
created by passing in the X and Y coordinates into its constructor:

```pycon
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(5, 23)
```

これらのコンストラクタはすべて、キーワード引数 `srid` を受け取ります。例えば:

```pycon
>>> from django.contrib.gis.geos import GEOSGeometry, LineString, Point
>>> print(GEOSGeometry("POINT (0 0)", srid=4326))
SRID=4326;POINT (0 0)
>>> print(LineString((0, 0), (1, 1), srid=4326))
SRID=4326;LINESTRING (0 0, 1 1)
>>> print(Point(0, 0, srid=32140))
SRID=32140;POINT (0 0)
```

最後に、ファイルから [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトを返す [`fromfile()`](#django.contrib.gis.geos.fromfile) ファクトリーメソッドがあります:

```pycon
>>> from django.contrib.gis.geos import fromfile
>>> pnt = fromfile("/path/to/pnt.wkt")
>>> pnt = fromfile(open("/path/to/pnt.wkt"))
```

> **ログが GEOS 関連のエラーで一杯です**
>
> Webサーバーのログファイルに、多くの `TypeError` や `AttributeError` 例外が記録されます。これは通常、Pythonモジュールのトップレベルで GEOS オブジェクトを作成していることを意味します。そして、ガベージコレクタの競合状態のために、あなたのモジュールは GEOS オブジェクトよりも先にガベージコレクションされます。これを防ぐには、関数/メソッドのローカルスコープ内に [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトを作成します。

### ジオメトリは Pythonic です

[`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトは 'Pythonic' (Pythonらしい) であり、要素には標準的な Python 形式を使ってアクセスしたり変更したり、イテレートしたりできます。例えば、[`Point`](#django.contrib.gis.geos.Point) の座標をイテレートできます:

```pycon
>>> pnt = Point(5, 23)
>>> [coord for coord in pnt]
[5.0, 23.0]
```

ジオメトリオブジェクトの [`GEOSGeometry.coords`](#django.contrib.gis.geos.GEOSGeometry.coords) プロパティを使用すると、ジオメトリ座標を Python のタプルで取得できます:

```pycon
>>> pnt.coords
(5.0, 23.0)
```

You can get/set geometry components using standard Python indexing
techniques. However, what is returned depends on the geometry type
of the object. For example, indexing on a [`LineString`](#django.contrib.gis.geos.LineString)
returns a coordinate tuple:

```pycon
>>> from django.contrib.gis.geos import LineString
>>> line = LineString((0, 0), (0, 50), (50, 50), (50, 0), (0, 0))
>>> line[0]
(0.0, 0.0)
>>> line[-2]
(50.0, 0.0)
```

[`Polygon`](#django.contrib.gis.geos.Polygon) 上のインデックス指定は、そのインデックスに対応するリング ([`LinearRing`](#django.contrib.gis.geos.LinearRing) オブジェクト) を返します:

```pycon
>>> from django.contrib.gis.geos import Polygon
>>> poly = Polygon(((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)))
>>> poly[0]
<LinearRing object at 0x1044395b0>
>>> poly[0][-2]  # second-to-last coordinate of external ring
(50.0, 0.0)
```

さらに、ジオメトリの座標や要素はPythonのリストのように追加や変更が可能です:

```pycon
>>> line[0] = (1.0, 1.0)
>>> line.pop()
(0.0, 0.0)
>>> line.append((1.0, 1.0))
>>> line.coords
((1.0, 1.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (1.0, 1.0))
```

ジオメトリーは set のような演算子をサポートします:

```pycon
>>> from django.contrib.gis.geos import LineString
>>> ls1 = LineString((0, 0), (2, 2))
>>> ls2 = LineString((1, 1), (3, 3))
>>> print(ls1 | ls2)  # equivalent to `ls1.union(ls2)`
MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (2 2, 3 3))
>>> print(ls1 & ls2)  # equivalent to `ls1.intersection(ls2)`
LINESTRING (1 1, 2 2)
>>> print(ls1 - ls2)  # equivalent to `ls1.difference(ls2)`
LINESTRING(0 0, 1 1)
>>> print(ls1 ^ ls2)  # equivalent to `ls1.sym_difference(ls2)`
MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))
```

> **等値演算子は空間的な等価性をチェックしません**
>
> [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) の等値演算子は、[`equals_exact()`](#django.contrib.gis.geos.GEOSGeometry.equals_exact) を使用します。[`equals()`](#django.contrib.gis.geos.GEOSGeometry.equals) ではありません。つまり、比較されるジオメトリは、同じ座標、同じ位置、同じSRID を持つ必要があります。
>
> ```pycon
> >>> from django.contrib.gis.geos import LineString
> >>> ls1 = LineString((0, 0), (1, 1))
> >>> ls2 = LineString((1, 1), (0, 0))
> >>> ls3 = LineString((1, 1), (0, 0), srid=4326)
> >>> ls1.equals(ls2)
> True
> >>> ls1 == ls2
> False
> >>> ls3 == ls2  # different SRIDs
> False
> ```

## ジオメトリ・オブジェクト

### `GEOSGeometry`

#### `class GEOSGeometry(geo_input, srid=None, * (Keyword-only parameters separator (PEP 3102)), max_geom_collections=198)`

**パラメータ:** - `geo_input` -- ジオメトリ入力値 (文字列または [`memoryview`](https://docs.python.org/3/library/stdtypes.html#memoryview))
- `srid` ([`int`](https://docs.python.org/3/library/functions.html#int)) -- 空間参照識別子
- `max_geom_collections` -- maximum number of nested (WKT) or total (WKB)
  geometry collections accepted before parsing is refused

This is the base class for all GEOS geometry objects. It initializes on the
given `geo_input` argument, and then assumes the proper geometry subclass
(e.g., `GEOSGeometry('POINT(1 1)')` will create a [`Point`](#django.contrib.gis.geos.Point) object).

パラメータ `srid` が与えられた場合、 `geo_input` が SRID を持っていない場合は、作成されたジオメトリの SRID が設定されます。パラメータ `geo_input` と `srid` で異なる SRID が指定された場合、 `ValueError` が発生します:

```pycon
>>> from django.contrib.gis.geos import GEOSGeometry
>>> GEOSGeometry("POINT EMPTY", srid=4326).ewkt
'SRID=4326;POINT EMPTY'
>>> GEOSGeometry("SRID=4326;POINT EMPTY", srid=4326).ewkt
'SRID=4326;POINT EMPTY'
>>> GEOSGeometry("SRID=1;POINT EMPTY", srid=4326)
Traceback (most recent call last):
...
ValueError: Input geometry already has SRID: 1.
```

以下の入力形式とそれに対応するPythonの型を受け付けます:

| フォーマット | 入力の型 |
| --- | --- |
| WKT / EWKT | `str` |
| HEX / HEXEWKB | `str` |
| WKB / EWKB | `memoryview` |
| [**GeoJSON**](https://datatracker.ietf.org/doc/html/rfc7946.html) | `str` |

GeoJSON形式では、SRIDは `crs` メンバーに基づいて設定されます。もし `crs` が指定されていない場合、SRIDはデフォルトで4326になります。

> **Changed in Django 5.2.17**
>
> The `max_geom_collections` parameter was added.

#### `classmethod GEOSGeometry.from_gml(gml_string)`

与えられたGML文字列から [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を構築します。

#### プロパティ

#### `GEOSGeometry.coords`

ジオメトリの座標をタプルで返します。

#### `GEOSGeometry.dims`

ジオメトリの次元を返します:

- [`Point`](#django.contrib.gis.geos.Point) および [`MultiPoint`](#django.contrib.gis.geos.MultiPoint) に対しては `0`
- [`LineString`](#django.contrib.gis.geos.LineString) および [`MultiLineString`](#django.contrib.gis.geos.MultiLineString) に対しては `1`
- [`Polygon`](#django.contrib.gis.geos.Polygon) および [`MultiPolygon`](#django.contrib.gis.geos.MultiPolygon) に対しては `2` です。
- [`GeometryCollection`](#django.contrib.gis.geos.GeometryCollection) が空の場合は `-1` を返します。
- 空ではない [`GeometryCollection`](#django.contrib.gis.geos.GeometryCollection) の要素の最大次元

#### `GEOSGeometry.empty`

ジオメトリ内の点の集合が空であるかどうかを返します。

#### `GEOSGeometry.geom_type`

Returns a string corresponding to the type of geometry. For example:

```pycon
>>> pnt = GEOSGeometry("POINT(5 23)")
>>> pnt.geom_type
'Point'
```

#### `GEOSGeometry.geom_typeid`

Returns the GEOS geometry type identification number. The following table
shows the value for each geometry type:

| ジオメトリ | ID |
| --- | --- |
| [`Point`](#django.contrib.gis.geos.Point) | 0 |
| [`LineString`](#django.contrib.gis.geos.LineString) | 1 |
| [`LinearRing`](#django.contrib.gis.geos.LinearRing) | 2 |
| [`Polygon`](#django.contrib.gis.geos.Polygon) | 3 |
| [`MultiPoint`](#django.contrib.gis.geos.MultiPoint) | 4 |
| [`MultiLineString`](#django.contrib.gis.geos.MultiLineString) | 5 |
| [`MultiPolygon`](#django.contrib.gis.geos.MultiPolygon) | 6 |
| [`GeometryCollection`](#django.contrib.gis.geos.GeometryCollection) | 7 |

#### `GEOSGeometry.num_coords`

ジオメトリ内の座標の数を返します。

#### `GEOSGeometry.num_geom`

Returns the number of geometries in this geometry. In other words, will
return 1 on anything but geometry collections.

#### `GEOSGeometry.hasz`

Returns a boolean indicating whether the geometry has the Z dimension.

#### `GEOSGeometry.hasm`

> **New in Django 6.0**

Returns a boolean indicating whether the geometry has the M dimension.
Requires GEOS 3.12.

#### `GEOSGeometry.ring`

ジオメトリが `LinearRing` であるかどうかを示す真偽値を返します。

#### `GEOSGeometry.simple`

Returns a boolean indicating whether the geometry is 'simple'. A geometry
is simple if and only if it does not intersect itself (except at boundary
points). For example, a [`LineString`](#django.contrib.gis.geos.LineString) object is not simple if it
intersects itself. Thus, [`LinearRing`](#django.contrib.gis.geos.LinearRing) and [`Polygon`](#django.contrib.gis.geos.Polygon) objects
are always simple because they cannot intersect themselves, by definition.

#### `GEOSGeometry.valid`

ジオメトリが有効かどうかを示すブール値を返します。

#### `GEOSGeometry.valid_reason`

ジオメトリが無効である理由を表す文字列を返します。

#### `GEOSGeometry.srid`

Property that may be used to retrieve or set the SRID associated with the
geometry. For example:

```pycon
>>> pnt = Point(5, 23)
>>> print(pnt.srid)
None
>>> pnt.srid = 4326
>>> pnt.srid
4326
```

#### 出力プロパティ

The properties in this section export the [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) object into
a different. This output may be in the form of a string, buffer, or even
another object.

#### `GEOSGeometry.ewkt`

Returns the "extended" Well-Known Text of the geometry. This representation
is specific to PostGIS and is a superset of the OGC WKT standard. [^1]
Essentially the SRID is prepended to the WKT representation, for example
`SRID=4326;POINT(5 23)`.

> **Note**
>
> このプロパティからの出力には、PostGISがそのEWKT表現でサポートする3dm、3dz、および4d情報は含まれません。

#### `GEOSGeometry.hex`

Returns the WKB of this Geometry in hexadecimal form. Please note
that the SRID value is not included in this representation
because it is not a part of the OGC specification (use the
[`GEOSGeometry.hexewkb`](#django.contrib.gis.geos.GEOSGeometry.hexewkb) property instead).

#### `GEOSGeometry.hexewkb`

Returns the EWKB of this Geometry in hexadecimal form. This is an
extension of the WKB specification that includes the SRID value
that are a part of this geometry.

#### `GEOSGeometry.json`

ジオメトリの GeoJSON 表現を返します。結果は完全な GeoJSON 構造ではなく、GeoJSON 構造の `geometry` キーの内容のみです。 [GeoJSON シリアライザ](/ja/6.0/ref/contrib/gis/serializers/) も参照してください。

#### `GEOSGeometry.geojson`

[`GEOSGeometry.json`](#django.contrib.gis.geos.GEOSGeometry.json) のエイリアス。

#### `GEOSGeometry.kml`

Returns a [KML](https://developers.google.com/kml/documentation/) (Keyhole Markup Language) representation of the
geometry. This should only be used for geometries with an SRID of
4326 (WGS84), but this restriction is not enforced.

#### `GEOSGeometry.ogr`

GEOS ジオメトリに対応する [`OGRGeometry`](/ja/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.OGRGeometry) オブジェクトを返します。

#### `GEOSGeometry.wkb`

Returns the WKB (Well-Known Binary) representation of this Geometry
as a Python buffer. SRID value is not included, use the
[`GEOSGeometry.ewkb`](#django.contrib.gis.geos.GEOSGeometry.ewkb) property instead.

#### `GEOSGeometry.ewkb`

このジオメトリの EWKB 表現を Python バッファとして返します。これは、このジオメトリの一部であるすべての SRID 値を含む WKB 仕様の拡張です。

#### `GEOSGeometry.wkt`

ジオメトリの Well-Known Text (OGC標準) を返します。

#### 空間述語メソッド (Spatial Predicate Method)

以下の空間述語 (predicate) メソッドは、別の [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) インスタンス (`other`) をパラメータとして受け取り、真偽値を返します。

#### `GEOSGeometry.contains(other)`

[`other.within(this)`](#django.contrib.gis.geos.GEOSGeometry.within) が `True` を返した場合に `True` を返します。

#### `GEOSGeometry.covers(other)`

このジオメトリが指定したジオメトリをカバーしている場合は `True` を返します。

`covers` predicate は以下の定義と等価です:

- もう一方のジオメトリのすべての点は、このジオメトリの点である。
- 2つのジオメトリの [DE-9IM](https://en.wikipedia.org/wiki/DE-9IM) 交差行列は `T*****FF*`, `*T****FF*`, `***T**FF*`, または `****T*FF*` である。

どちらかのジオメトリが空の場合、`False` を返します。

この predicate は [`GEOSGeometry.contains()`](#django.contrib.gis.geos.GEOSGeometry.contains) に似ていますが、より包括的です (つまり、より多くの場合に `True` を返します) 。特に、 [`contains()`](#django.contrib.gis.geos.GEOSGeometry.contains) とは異なり、ジオメトリの境界と内部の点を区別しません。ほとんどの場合、 [`contains()`](#django.contrib.gis.geos.GEOSGeometry.contains) よりも `covers()` を優先すべきです。さらに、 `covers()` は最適化しやすいので、 [`contains()`](#django.contrib.gis.geos.GEOSGeometry.contains) よりも優れています。

#### `GEOSGeometry.crosses(other)`

2つのジオメトリの DE-9IM 交差行列が `T*T*******` (点と曲線、点と面積、線と面積の場合) `0********` (2つの曲線の場合) である場合に `True` を返します。

#### `GEOSGeometry.disjoint(other)`

2つのジオメトリのDE-9IM交差行列が `FF*FF****` である場合に `True` を返します。

#### `GEOSGeometry.equals(other)`

2つのジオメトリの DE-9IM 交差行列が `T*F**FFF*` である場合に `True` を返します。

#### `GEOSGeometry.equals_exact(other, tolerance=0)`

Returns true if the two geometries are exactly equal, up to a
specified tolerance. The `tolerance` value should be a floating
point number representing the error tolerance in the comparison, e.g.,
`poly1.equals_exact(poly2, 0.001)` will compare equality to within
one thousandth of a unit.

#### `GEOSGeometry.equals_identical(other)`

すべての次元において、すべての頂点の構造、順序、および値が同じであることをチェックすることにより、2つのジオメトリが点的に等価である場合に `True` を返します。 `NaN` 値は他の `NaN` 値と等しいとみなされます。GEOS 3.12が必要です。

#### `GEOSGeometry.intersects(other)`

[`GEOSGeometry.disjoint()`](#django.contrib.gis.geos.GEOSGeometry.disjoint) が `False` の場合に `True` を返します。

#### `GEOSGeometry.overlaps(other)`

2つのジオメトリの DE-9IM 交差行列が `T*T***T**` (2つの点または2つの曲面の場合) `1*T***T**` (2つの曲線の場合) である場合に true を返します．

#### `GEOSGeometry.relate_pattern(other, pattern)`

このジオメトリと他のジオメトリの DE-9IM 交差行列の要素が、与えられた `pattern` -- アルファベットから 9 文字の文字列と一致する場合に `True` を返します: {`T`, `F`, `*`, `0`} の9文字の文字列。

#### `GEOSGeometry.touches(other)`

2つのジオメトリのDE-9IM交差行列が `FT*******`, `F**T*****` または `F***T****` である場合に `True` を返します。

#### `GEOSGeometry.within(other)`

2つのジオメトリの DE-9IM 交差行列が `T*F**F***` である場合に `True` を返します。

#### トポロジー的 (Topological) メソッド

#### `GEOSGeometry.buffer(width, quadsegs=8)`

このジオメトリからの距離が与えられた `width` 以下のすべての点を表す [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返す。オプションの `quadsegs` キーワードは、1/4円の近似に使用するセグメントの数を設定します (デフォルトは8) 。

#### `GEOSGeometry.buffer_with_style(width, quadsegs=8, end_cap_style=1, join_style=1, mitre_limit=5.0)`

[`buffer()`](#django.contrib.gis.geos.GEOSGeometry.buffer) と同じですが、バッファのスタイルをカスタマイズできます。

- `end_cap_style` には、round (`1`) 、flat (`2`) 、square (`3`) を指定できます。
- `join_style` には round (`1`) 、mitre (`2`) 、bevel (`3`) のいずれかを指定します。
- Mitre レシオ制限 (`mitre_limit`) は、mitre 形式の結合スタイルにのみ影響します。

#### `GEOSGeometry.difference(other)`

このジオメトリを構成する点のうち、他の点を構成しない点を表す [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返す。

#### `GEOSGeometry.interpolate(distance)`

#### `GEOSGeometry.interpolate_normalized(distance)`

与えられた距離 (float) に対する、その距離上の点 (またはもっとも近い点) を返します。この距離は、 [`LineString`](#django.contrib.gis.geos.LineString) または [`MultiLineString`](#django.contrib.gis.geos.MultiLineString) のジオメトリであり、正規化されたバージョンでは、0 (始点) から 1 (終点) の値を取る float 型として距離の値を取ります。

[`GEOSGeometry.project()`](#django.contrib.gis.geos.GEOSGeometry.project) の逆。

#### `GEOSGeometry.intersection(other)`

このジオメトリと他のジオメトリが共有する点を表す [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返す。

#### `GEOSGeometry.project(point)`

#### `GEOSGeometry.project_normalized(point)`

ジオメトリ ([`LineString`](#django.contrib.gis.geos.LineString) または [`MultiLineString`](#django.contrib.gis.geos.MultiLineString)) の原点から、ジオメトリに投影された点 (つまり、指定された点に最も近い線の点) までの距離 (float) を返します。正規化されたバージョンは、0 (原点) と 1 (終点) の間の浮動小数点数として距離を返します。

[`GEOSGeometry.interpolate()`](#django.contrib.gis.geos.GEOSGeometry.interpolate) の逆。

#### `GEOSGeometry.relate(other)`

このジオメトリと他のジオメトリのトポロジカルな関係を表す DE-9IM 交差行列 (文字列) を返します。

#### `GEOSGeometry.simplify(tolerance=0.0, preserve_topology=False)`

新しい [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返します。これは Douglas-Peucker アルゴリズムを用いて、指定した許容誤差まで単純化されます。許容誤差の値が大きいほど、出力される点は少なくなります。公差が指定されない場合、デフォルトは0です。

デフォルトでは、この関数はトポロジーを保持しません。例えば、 [`Polygon`](#django.contrib.gis.geos.Polygon) オブジェクトは分割されたり、線に折りたたまれたり、消えたりすることがあります。 [`Polygon`](#django.contrib.gis.geos.Polygon) の穴ができたり消えたり、線が交差したりすることがあります。 `preserve_topology=True` を指定することで、結果は入力と同じ次元と要素数になります。

#### `GEOSGeometry.sym_difference(other)`

このジオメトリに含まれる点のうち他のジオメトリに含まれない点と、他のジオメトリに含まれる点のうちこのジオメトリに含まれない点を結合した [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返す。

#### `GEOSGeometry.union(other)`

このジオメトリともう一方のジオメトリのすべての点を表す [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返す。

#### トポロジー的プロパティ

#### `GEOSGeometry.boundary`

新しく割り当てられた Geometry オブジェクトとして境界を返します。

#### `GEOSGeometry.centroid`

Returns a [`Point`](#django.contrib.gis.geos.Point) object representing the geometric center of
the geometry. The point is not guaranteed to be on the interior
of the geometry.

#### `GEOSGeometry.convex_hull`

ジオメトリ内のすべての点を含む最小の [`Polygon`](#django.contrib.gis.geos.Polygon) を返す。

#### `GEOSGeometry.envelope`

このジオメトリの外接を表す [`Polygon`](#django.contrib.gis.geos.Polygon) を返します。入力ジオメトリが点の場合は [`Point`](#django.contrib.gis.geos.Point) を返すこともあります。

#### `GEOSGeometry.point_on_surface`

このジオメトリ内部に存在することが保証された [`Point`](#django.contrib.gis.geos.Point) を計算し、返します。

#### `GEOSGeometry.unary_union`

このジオメトリのすべての要素の和集合を計算します。

結果は次の規約に従います:

- [`LineString`](#django.contrib.gis.geos.LineString) の集合を結合することは、ラインワークを完全にノード化し、分解する効果がある。
- [`Polygon`](#django.contrib.gis.geos.Polygon) のセットを結合すると常に [`Polygon`](#django.contrib.gis.geos.Polygon) または [`MultiPolygon`](#django.contrib.gis.geos.MultiPolygon) ジオメトリが返されます ([`GEOSGeometry.union()`](#django.contrib.gis.geos.GEOSGeometry.union) は、トポロジーの崩壊が発生した場合、次元が低いジオメトリが返される可能性がある点が異なります)。

#### その他のプロパティとメソッド

#### `GEOSGeometry.area`

このプロパティは、ジオメトリの面積を返します。

#### `GEOSGeometry.extent`

このプロパティは、このジオメトリの範囲を `(xmin, ymin, xmax, ymax)` からなる4タプルで返します。

#### `GEOSGeometry.clone()`

このメソッドは、元のジオメトリのクローンである [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) を返します。

#### `GEOSGeometry.distance(geom)`

このジオメトリと与えられた `geom` (別の [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクト) との間の最も近い点の距離を返します。

> **Note**
>
> GEOSの距離計算は線形です。言い換えると、GEOSはSRIDが地理座標系を指定していても球面計算を行いません。

#### `GEOSGeometry.length`

このジオメトリの長さを返します (例: [`Point`](#django.contrib.gis.geos.Point) の場合は 0、[`LineString`](#django.contrib.gis.geos.LineString) の長さ、または [`Polygon`](#django.contrib.gis.geos.Polygon) の円周)。

#### `GEOSGeometry.prepared`

このジオメトリの内容の GEOS `PreparedGeometry` を返します。 `PreparedGeometry` オブジェクトは、contains、intersects、covers、crosss、disjoint、overlaps、touches、および within の操作に最適化されています。詳細は [準備された (Prepared) ジオメトリ](#prepared-geometries) のドキュメントを参照してください。

#### `GEOSGeometry.srs`

ジオメトリの SRID に対応する [`SpatialReference`](/ja/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.SpatialReference) オブジェクト、または `None` を返します。

#### `GEOSGeometry.transform(ct, clone=False)`

与えられた座標変換パラメータ (`ct`) に従ってジオメトリを変換します。座標変換パラメータは、整数の SRID、空間参照の WKT 文字列、PROJ 文字列、 [`SpatialReference`](/ja/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.SpatialReference) オブジェクト、 [`CoordTransform`](/ja/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.CoordTransform) オブジェクトのいずれかです。デフォルトでは、ジオメトリはその場で変換され、何も返されません。しかし、 `clone` キーワードが設定されている場合、ジオメトリは変更されず、変換されたジオメトリのクローンが返されます。

> **Note**
>
> GDALが利用できないか、ジオメトリのSRIDが `None` または0未満の場合に [`GEOSException`](#django.contrib.gis.geos.GEOSException) を発生させます。 [`CoordTransform`](/ja/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.CoordTransform) オブジェクトを使用して呼び出される場合、ジオメトリのSRIDには制約を与えません。

#### `GEOSGeometry.make_valid()`

入力された頂点を失わず、有効な [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) に変換して返します。既に有効なジオメトリの場合、そのまま返されます。これは [`MakeValid`](/ja/6.0/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.MakeValid) データベース関数に類似しています。GEOS 3.8が必要です。

#### `GEOSGeometry.normalize(clone=False)`

このジオメトリを正規形に変換します。 `clone` キーワードがセットされている場合、ジオメトリは変更されず、代わりにジオメトリの正規化されたクローンが返されます:

```pycon
>>> g = MultiPoint(Point(0, 0), Point(2, 2), Point(1, 1))
>>> print(g)
MULTIPOINT (0 0, 2 2, 1 1)
>>> g.normalize()
>>> print(g)
MULTIPOINT (2 2, 1 1, 0 0)
```

### `Point`

#### `class Point(x=None, y=None, z=None, srid=None)`

`Point` オブジェクトは、ポイントの座標を表す引数を使用してインスタンス化されたり、1 つのシーケンス座標でインスタンス化されたりします。例えば、以下は同等です:

```pycon
>>> pnt = Point(5, 23)
>>> pnt = Point([5, 23])
```

空の `Point` オブジェクトを引数なしまたは空のシーケンスを渡すことでインスタンス化できます。以下は同等です:

```pycon
>>> pnt = Point()
>>> pnt = Point([])
```

### `LineString`

#### `class LineString(*args, **kwargs)`

`LineString` オブジェクトは、座標のシーケンスまたは [`Point`](#django.contrib.gis.geos.Point) オブジェクトを引数としてインスタンス化されます。例えば、以下は同等です:

```pycon
>>> ls = LineString((0, 0), (1, 1))
>>> ls = LineString(Point(0, 0), Point(1, 1))
```

さらに、`LineString` オブジェクトは、座標または [`Point`](#django.contrib.gis.geos.Point) オブジェクトのシーケンスを渡すことによっても作成できます:

```pycon
>>> ls = LineString(((0, 0), (1, 1)))
>>> ls = LineString([Point(0, 0), Point(1, 1)])
```

空の `LineString` オブジェクトは、引数を渡さないか空のシーケンスを渡すことでインスタンス化できます。以下は同等です:

```pycon
>>> ls = LineString()
>>> ls = LineString([])
```

#### `closed`

この `LineString` が閉じているかどうかを返します。

### `LinearRing`

#### `class LinearRing(*args, **kwargs)`

`LinearRing` オブジェクトは、[`LineString`](#django.contrib.gis.geos.LineString) オブジェクトとまったく同じ方法で構築されますが、座標は *閉じている* 必要があります。つまり、最初の座標と最後の座標が同じである必要があります。例:

```pycon
>>> ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))
```

`(0, 0)` は最初で最後の座標です。もし等しくなければ、エラーが発生します。

#### `is_counterclockwise`

この `LinearRing` が反時計回りかどうかを返します。

### `Polygon`

#### `class Polygon(*args, **kwargs)`

`Polygon` objects may be instantiated by passing in parameters that
represent the rings of the polygon. The parameters must either be
[`LinearRing`](#django.contrib.gis.geos.LinearRing) instances, or a sequence that may be used to construct
a [`LinearRing`](#django.contrib.gis.geos.LinearRing):

```pycon
>>> ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
>>> int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))
>>> poly = Polygon(ext_coords, int_coords)
>>> poly = Polygon(LinearRing(ext_coords), LinearRing(int_coords))
```

#### `classmethod from_bbox(bbox)`

与えられたバウンディングボックスからポリゴンオブジェクトを返します。ポリゴンオブジェクトは `(xmin, ymin, xmax, ymax)` からなる4タプルで構成されます。

#### `num_interior_rings`

このジオメトリの内部リングの数を返します。

> **ポリゴン（多角形）の比較**
>
> ポリゴンのオブジェクトを直接 `<` や `>` と比較することも可能ですが、比較はポリゴンの [`LineString`](#django.contrib.gis.geos.LineString) を通して行われるため、あまり意味がないことに注意してください (しかし、一貫性があり、素早く比較できます) 。 [`area`](#django.contrib.gis.geos.GEOSGeometry.area) プロパティで常に強制的に比較することもできます:
>
> ```pycon
> >>> if poly_1.area > poly_2.area:
> ...     pass
> ...
> ```

## ジオメトリのコレクション

### `MultiPoint`

#### `class MultiPoint(*args, **kwargs)`

`MultiPoint` オブジェクトは [`Point`](#django.contrib.gis.geos.Point) オブジェクトを引数として渡すか、1つの [`Point`](#django.contrib.gis.geos.Point) オブジェクトのシーケンスを渡すことでインスタンス化できます。

```pycon
>>> mp = MultiPoint(Point(0, 0), Point(1, 1))
>>> mp = MultiPoint((Point(0, 0), Point(1, 1)))
```

### `MultiLineString`

#### `class MultiLineString(*args, **kwargs)`

`MultiLineString` オブジェクトは、 [`LineString`](#django.contrib.gis.geos.LineString) オブジェクトを引数として渡すか、1つの [`LineString`](#django.contrib.gis.geos.LineString) オブジェクトのシーケンスを渡すことでインスタンス化が可能です。

```pycon
>>> ls1 = LineString((0, 0), (1, 1))
>>> ls2 = LineString((2, 2), (3, 3))
>>> mls = MultiLineString(ls1, ls2)
>>> mls = MultiLineString([ls1, ls2])
```

#### `merged`

この `MultiLineString` のすべてのコンポーネントを結合した線を表す [`LineString`](#django.contrib.gis.geos.LineString) を返します。

#### `closed`

すべての要素が閉じられている場合にのみ `True` を返します。

### `MultiPolygon`

#### `class MultiPolygon(*args, **kwargs)`

`MultiPolygon` オブジェクトは、 [`Polygon`](#django.contrib.gis.geos.Polygon) オブジェクトを引数として渡すか、1つの [`Polygon`](#django.contrib.gis.geos.Polygon) オブジェクトのシーケンスを渡すことでインスタンス化できます。

```pycon
>>> p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0)))
>>> p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1)))
>>> mp = MultiPolygon(p1, p2)
>>> mp = MultiPolygon([p1, p2])
```

### `GeometryCollection`

#### `class GeometryCollection(*args, **kwargs)`

`GeometryCollection` オブジェクトは、他の [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトを引数として渡すか、または単一の [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) オブジェクトのシーケンスを引数として渡すことでインスタンス化できます:

```pycon
>>> poly = Polygon(((0, 0), (0, 1), (1, 1), (0, 0)))
>>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
>>> gc = GeometryCollection((Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly))
```

## 準備された (Prepared) ジオメトリ

In order to obtain a prepared geometry, access the
[`GEOSGeometry.prepared`](#django.contrib.gis.geos.GEOSGeometry.prepared) property. Once you have a `PreparedGeometry`
instance its spatial predicate methods, listed below, may be used with other
`GEOSGeometry` objects. An operation with a prepared geometry can be orders
of magnitude faster -- the more complex the geometry that is prepared, the
larger the speedup in the operation. For more information, please consult the
[GEOS wiki page on prepared geometries](https://trac.osgeo.org/geos/wiki/PreparedGeometry).

例:

```pycon
>>> from django.contrib.gis.geos import Point, Polygon
>>> poly = Polygon.from_bbox((0, 0, 5, 5))
>>> prep_poly = poly.prepared
>>> prep_poly.contains(Point(2.5, 2.5))
True
```

### `PreparedGeometry`

#### `class PreparedGeometry`

`PreparedGeometry` のすべてのメソッドは `other` 引数を取ります。この引数は [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) インスタンスでなければなりません。

#### `contains(other)`

#### `contains_properly(other)`

#### `covers(other)`

#### `crosses(other)`

#### `disjoint(other)`

#### `intersects(other)`

#### `overlaps(other)`

#### `touches(other)`

#### `within(other)`

## ジオメトリ ファクトリ

#### `fromfile(file_h)`

**パラメータ:** `file_h` (a Python `file` object or a string path to the file) -- 空間データを含む入力ファイル

**戻り値の型:** ファイル内の空間データに対応する [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry)

例:

```pycon
>>> from django.contrib.gis.geos import fromfile
>>> g = fromfile("/home/bob/geom.wkt")
```

#### `fromstr(string, srid=None)`

**パラメータ:** - `string` ([`str`](https://docs.python.org/3/library/stdtypes.html#str)) -- 空間データを含む文字列
- `srid` ([`int`](https://docs.python.org/3/library/functions.html#int)) -- 空間参照識別子

**戻り値の型:** 文字列内の空間データに対応する [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry)

`fromstr(string, srid)` は、 [`GEOSGeometry(string, srid)`](#django.contrib.gis.geos.GEOSGeometry) と同等です。

例:

```pycon
>>> from django.contrib.gis.geos import fromstr
>>> pnt = fromstr("POINT(-90.5 29.5)", srid=4326)
```

## I/O オブジェクト

### Reader オブジェクト

Reader I/Oクラスは、 `read(geom)` メソッドに渡されたWKBおよび/またはWKT入力から [`GEOSGeometry`](#django.contrib.gis.geos.GEOSGeometry) インスタンスを返します。

#### `class WKBReader`

例:

```pycon
>>> from django.contrib.gis.geos import WKBReader
>>> wkb_r = WKBReader()
>>> wkb_r.read("0101000000000000000000F03F000000000000F03F")
<Point object at 0x103a88910>
```

#### `class WKTReader`

例:

```pycon
>>> from django.contrib.gis.geos import WKTReader
>>> wkt_r = WKTReader()
>>> wkt_r.read("POINT(1 1)")
<Point object at 0x103a88b50>
```

### Writer オブジェクト

All writer objects have a `write(geom)` method that returns either the
WKB or WKT of the given geometry. In addition, [`WKBWriter`](#django.contrib.gis.geos.WKBWriter) objects
also have properties that may be used to change the byte order, and or
include the SRID value (in other words, EWKB).

#### `class WKBWriter(dim=2)`

`WKBWriter` provides the most control over its output. By default it
returns OGC-compliant WKB when its `write` method is called. However,
it has properties that allow for the creation of EWKB, a superset of the
WKB standard that includes additional information. See the
[`WKBWriter.outdim`](#django.contrib.gis.geos.WKBWriter.outdim) documentation for more details about the `dim`
argument.

#### `write(geom)`

与えられたジオメトリの WKB を Python の `buffer` オブジェクトとして返します。例:

```pycon
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> pnt = Point(1, 1)
>>> wkb_w = WKBWriter()
>>> wkb_w.write(pnt)
<read-only buffer for 0x103a898f0, size -1, offset 0 at 0x103a89930>
```

#### `write_hex(geom)`

Returns WKB of the geometry in hexadecimal. Example:

```pycon
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> pnt = Point(1, 1)
>>> wkb_w = WKBWriter()
>>> wkb_w.write_hex(pnt)
'0101000000000000000000F03F000000000000F03F'
```

#### `byteorder`

このプロパティを設定すると、ジオメトリ表現のバイトオーダーを変更できます。

| バイトオーダーの値 | 説明 |
| --- | --- |
| 0 | ビッグエンディアン (例: RISC システムと互換性あり) |
| 1 | リトルエンディアン (例: x86 システムと互換性あり) |

例:

```pycon
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> pnt = Point(1, 1)
>>> wkb_w.write_hex(pnt)
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.byteorder = 0
'00000000013FF00000000000003FF0000000000000'
```

#### `outdim`

This property may be set to change the output dimension of the geometry
representation. In other words, if you have a 3D geometry then set to 3
so that the Z value is included in the WKB.

| Outdim 値 | 説明 |
| --- | --- |
| 2 | デフォルトで、2次元のWKBを出力します。 |
| 3 | 3D の WKB を出力します。 |

例:

```pycon
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> wkb_w.outdim
2
>>> pnt = Point(1, 1, 1)
>>> wkb_w.write_hex(pnt)  # By default, no Z value included:
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.outdim = 3  # Tell writer to include Z values
>>> wkb_w.write_hex(pnt)
'0101000080000000000000F03F000000000000F03F000000000000F03F'
```

#### `srid`

Set this property with a boolean to indicate whether the SRID of the
geometry should be included with the WKB representation. Example:

```pycon
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> wkb_w = WKBWriter()
>>> pnt = Point(1, 1, srid=4326)
>>> wkb_w.write_hex(pnt)  # By default, no SRID included:
'0101000000000000000000F03F000000000000F03F'
>>> wkb_w.srid = True  # Tell writer to include SRID
>>> wkb_w.write_hex(pnt)
'0101000020E6100000000000000000F03F000000000000F03F'
```

#### `class WKTWriter(dim=2, trim=False, precision=None)`

このクラスを使用すると、ジオメトリの WKT 表現を出力できます。コンストラクタの引数の詳細については [`WKBWriter.outdim`](#django.contrib.gis.geos.WKBWriter.outdim), [`trim`](#django.contrib.gis.geos.WKTWriter.trim), [`precision`](#django.contrib.gis.geos.WKTWriter.precision) 属性を参照してください。

#### `write(geom)`

与えられたジオメトリのWKTを返します。例:

```pycon
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1, 1)
>>> wkt_w = WKTWriter()
>>> wkt_w.write(pnt)
'POINT (1.0000000000000000 1.0000000000000000)'
```

#### `outdim`

[`WKBWriter.outdim`](#django.contrib.gis.geos.WKBWriter.outdim) を参照。

#### `trim`

このプロパティは、不要な小数のトリミングを有効または無効にするために使用されます。

```pycon
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1, 1)
>>> wkt_w = WKTWriter()
>>> wkt_w.trim
False
>>> wkt_w.write(pnt)
'POINT (1.0000000000000000 1.0000000000000000)'
>>> wkt_w.trim = True
>>> wkt_w.write(pnt)
'POINT (1 1)'
```

#### `precision`

このプロパティは座標の丸め精度をコントロールします。もし `None` に設定すると丸めは無効になります。

```
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1.44, 1.66)
>>> wkt_w = WKTWriter()
>>> print(wkt_w.precision)
None
>>> wkt_w.write(pnt)
'POINT (1.4399999999999999 1.6599999999999999)'
>>> wkt_w.precision = 0
>>> wkt_w.write(pnt)
'POINT (1 2)'
>>> wkt_w.precision = 1
>>> wkt_w.write(pnt)
'POINT (1.4 1.7)'
```

**脚注**

[^1]: *参照* [PostGIS EWKB, EWKT and Canonical Forms](https://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT), PostGIS documentation at Ch. 4.1.2.

## 設定

### `GEOS_LIBRARY_PATH`

A string specifying the location of the GEOS C library. Typically,
this setting is only used if the GEOS C library is in a non-standard
location (e.g., `/home/bob/lib/libgeos_c.so`).

> **Note**
>
> 設定は **C** 共有ライブラリの *フル* パスである必要があります。つまり、 `libgeos.so` ではなく `libgeos_c.so` を使用することが望まれます。

## 例外

#### `exception GEOSException`

ベースとなるGEOS例外。GEOS関連のエラーを示します。
