# Geometric Operations

In this section, we consider some of the common kinds of geometries that arises in applications. These include `Point`

, `LineString`

, `Polygon`

, `GeometryCollection`

, `MultiPolygon`

, `MultiPoint`

, and `MultiLineString`

.

## Geometry Creation

To create geometries of different types,

```
point = ArchGDAL.createpoint(1.0, 2.0)
linestring = ArchGDAL.createlinestring([(i,i+1) for i in 1.0:3.0])
linearring = ArchGDAL.createlinearring([(0.,0.), (0.,1.), (1.,1.)])
simplepolygon = ArchGDAL.createpolygon([(0.,0.), (0.,1.), (1.,1.)])
complexpolygon = ArchGDAL.createpolygon([[(0.,0.), (0.,j), (j,j)] for j in 1.0:-0.1:0.9])
multipoint = ArchGDAL.createlinearring([(0.,0.), (0.,1.), (1.,1.)])
multilinestring = ArchGDAL.createmultilinestring([[(i,i+1) for i in j:j+3] for j in 1.0:5.0:6.0])
multipolygon = ArchGDAL.createmultipolygon([[[(0.,0.), (0.,j), (j,j)]] for j in 1.0:-0.1:0.9])
```

Geometry: MULTIPOLYGON (((0 0,0 1,1 1)),((0 0,0.0 0.9,0.9 0.9)))

Alternatively, they can be assembled from their components.

```
point = ArchGDAL.createpoint()
ArchGDAL.addpoint!(point, 1.0, 2.0)
linestring = ArchGDAL.createlinestring()
for i in 1.0:3.0
ArchGDAL.addpoint!(linestring, i, i+1)
end
linearring = ArchGDAL.createlinearring()
for i in 1.0:3.0
ArchGDAL.addpoint!(linearring, i, i+1)
end
polygon = ArchGDAL.createpolygon()
for j in 1.0:-0.1:0.9
ring = ArchGDAL.createlinearring([(0.,0.), (0.,j), (j,j)])
ArchGDAL.addgeom!(polygon, ring)
end
multipoint = ArchGDAL.createmultipoint()
for i in 1.0:3.0
pt = ArchGDAL.createpoint(i, i+1)
ArchGDAL.addgeom!(multipoint, pt)
end
multilinestring = ArchGDAL.createmultilinestring()
for j in 1.0:5.0:6.0
line = ArchGDAL.createlinestring([(i,i+1) for i in j:j+3])
ArchGDAL.addgeom!(multilinestring, line)
end
multipolygon = ArchGDAL.createmultipolygon()
for j in 1.0:-0.1:0.9
poly = ArchGDAL.createpolygon([(0.,0.), (0.,j), (j,j)])
ArchGDAL.addgeom!(multipolygon, poly)
end
```

They can also be constructed from other data formats such as:

- Well-Known Binary (WKB):
`ArchGDAL.fromWKB`

`([0x01,0x01,...,0x27,0x41])`

- Well-Known Text (WKT):
`ArchGDAL.fromWKT("POINT (1 2)")`

- JavaScript Object Notation (JSON):
`ArchGDAL.fromJSON("""{"type":"Point","coordinates":[1,2]}""")`

## Geometry Modification

The following methods are commonly used for retrieving elements of a geometry.

`ArchGDAL.getcoorddim(geom)`

: dimension of the coordinates. Returns`0`

for an empty point`ArchGDAL.getspatialref(geom)`

`ArchGDAL.getx(geom, i)`

`ArchGDAL.gety(geom, i)`

`ArchGDAL.getz(geom, i)`

`ArchGDAL.getpoint(geom, i)`

`ArchGDAL.getgeom(geom, i)`

The following methods are commonly used for modifying or adding to a geometry.

`ArchGDAL.setcoorddim!(geom, dim)`

`ArchGDAL.setpointcount!(geom, n)`

`ArchGDAL.setpoint!(geom, i, x, y)`

`ArchGDAL.setpoint!(geom, i, x, y, z)`

`ArchGDAL.addpoint!(geom, x, y)`

`ArchGDAL.addpoint!(geom, x, y, z)`

`ArchGDAL.addgeom!(geom1, geom2)`

`ArchGDAL.removegeom!(geom, i)`

`ArchGDAL.removeallgeoms!(geom)`

## Unary Operations

The following is an non-exhaustive list of unary operations available for geometries.

### Attributes

`ArchGDAL.geomdim(geom)`

:`0`

for points,`1`

for lines and`2`

for surfaces`ArchGDAL.getcoorddim(geom)`

: dimension of the coordinates. Returns`0`

for an empty point`ArchGDAL.envelope(geom)`

: the bounding envelope for this geometry`ArchGDAL.envelope3d(geom)`

: the bounding envelope for this geometry`ArchGDAL.wkbsize(geom)`

: size (in bytes) of related binary representation`ArchGDAL.getgeomtype(geom)`

: geometry type code (in`OGRwkbGeometryType`

)`ArchGDAL.geomname(geom)`

: WKT name for geometry type`ArchGDAL.getspatialref(geom)`

: spatial reference system. May be`NULL`

`ArchGDAL.geomlength(geom)`

: the length of the geometry, or`0.0`

for unsupported types`ArchGDAL.geomarea(geom)`

: the area of the geometry, or`0.0`

for unsupported types

### Predicates

The following predicates return a `Bool`

.

`ArchGDAL.isempty(geom)`

`ArchGDAL.isvalid(geom)`

`ArchGDAL.issimple(geom)`

`ArchGDAL.isring(geom)`

`ArchGDAL.hascurvegeom(geom, nonlinear::Bool)`

### Immutable Operations

The following methods do not modify `geom`

.

`ArchGDAL.clone(geom)`

: a copy of the geometry with the original spatial reference system.`ArchGDAL.forceto(geom, targettype)`

: force the provided geometry to the specified geometry type.`ArchGDAL.simplify(geom, tol)`

: Compute a simplified geometry.`ArchGDAL.simplifypreservetopology(geom, tol)`

: Simplify the geometry while preserving topology.`ArchGDAL.delaunaytriangulation(geom, tol, onlyedges)`

: a delaunay triangulation of the vertices of the geometry.`ArchGDAL.boundary(geom)`

: the boundary of the geometry`ArchGDAL.convexhull(geom)`

: the convex hull of the geometry.`ArchGDAL.buffer(geom, dist, quadsegs)`

: a polygon containing the region within the buffer distance of the original geometry.`ArchGDAL.union(geom)`

: the union of the geometry using cascading`ArchGDAL.pointonsurface(geom)`

: Returns a point guaranteed to lie on the surface.`ArchGDAL.centroid(geom)`

: Compute the geometry centroid. It is not necessarily within the geometry.`ArchGDAL.pointalongline(geom, distance)`

: Fetch point at given distance along curve.`ArchGDAL.polygonize(geom)`

: Polygonizes a set of sparse edges.

### Mutable Operations

The following methods modifies the first argument `geom`

.

`ArchGDAL.setcoorddim!(geom, dim)`

: sets the explicit coordinate dimension.`ArchGDAL.flattento2d!(geom)`

: Convert geometry to strictly 2D.`ArchGDAL.closerings!(geom)`

: Force rings to be closed by adding the start point to the end.`ArchGDAL.transform!(geom, coordtransform)`

: Apply coordinate transformation to geometry.`ArchGDAL.segmentize!(geom, maxlength)`

: Modify the geometry such it has no segment longer than the given distance.`ArchGDAL.empty!(geom)`

: Clear geometry information.

### Export Formats

`ArchGDAL.toWKB(geom)`

`ArchGDAL.toISOWKB(geom)`

`ArchGDAL.toWKT(geom)`

`ArchGDAL.toISOWKT(geom)`

`ArchGDAL.toGML(geom)`

`ArchGDAL.toKML(geom)`

`ArchGDAL.toJSON(geom)`

## Binary Operations

The following is an non-exhaustive list of binary operations available for geometries.

### Predicates

The following predicates return a `Bool`

.

`ArchGDAL.intersects(g1, g2)`

`ArchGDAL.equals(g1, g2)`

`ArchGDAL.disjoint(g1, g2)`

`ArchGDAL.touches(g1, g2)`

`ArchGDAL.crosses(g1, g2)`

`ArchGDAL.within(g1, g2)`

`ArchGDAL.contains(g1, g2)`

`ArchGDAL.overlaps(g1, g2)`

### Prepared geometry

When repeatedly calling `ArchGDAL.intersects(g1, g2)`

and `ArchGDAL.contains(g1, g2)`

on the same geometry, for example, when intersecting 50 points with a 100 polygons, it is possible to increase performance by caching required–otherwise repeatedly calculated–information on geometries. You can do this by *preparing* a geometry by calling `ArchGDAL.preparegeom(geom)`

and using the resulting prepared geometry as the first argument for `ArchGDAL.intersects(g1, g2)`

or `ArchGDAL.contains(g1, g2)`

.

If you use a custom GDAL installation, you can check whether it supports prepared geometries by calling `ArchGDAL.has_preparedgeom_support()`

.

### Immutable Operations

The following methods do not mutate the input geomteries `g1`

and `g2`

.

`ArchGDAL.intersection(g1, g2)`

`ArchGDAL.union(g1, g2)`

`ArchGDAL.difference(g1, g2)`

`ArchGDAL.symdifference(g1, g2)`

### Mutable Operations

The following method modifies the first argument `g1`

.