GEOS APILink to this heading

BakgrundLink to this heading

Vad är GEOS?Link to this heading

GEOS stands for Geometry Engine - Open Source, and is a C++ library, ported from the Java Topology Suite. GEOS implements the OpenGIS Simple Features for SQL spatial predicate functions and spatial operators. GEOS, now an OSGeo project, was initially developed and maintained by Refractions Research of Victoria, Canada.

FunktionerLink to this heading

GeoDjango implementerar en Python-wrapper på hög nivå för GEOS-biblioteket, med bland annat följande funktioner:

  • Ett BSD-licensierat gränssnitt till GEOS geometrirutiner, implementerat enbart i Python med hjälp av ctypes.

  • Loosely-coupled to GeoDjango. For example, GEOSGeometry objects may be used outside of a Django project/application. In other words, no need to have DJANGO_SETTINGS_MODULE set or use a database, etc.

  • Ändringsbarhet: GEOSGeometry-objekt kan ändras.

  • Testad på flera plattformar.

HandledningLink to this heading

Detta avsnitt innehåller en kort introduktion och handledning i hur man använder GEOSGeometry-objekt.

Skapa en geometriLink to this heading

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:

Python console
>>> 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 object may be created by passing in the X and Y coordinates into its constructor:

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

Alla dessa konstruktörer tar nyckelordsargumentet srid. Till exempel:

Python console
>>> 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)

Slutligen finns fabriksmetoden fromfile() som returnerar ett GEOSGeometry-objekt från en fil:

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

Geometrier är pytoniskaLink to this heading

GEOSGeometry-objekt är ”pythoniska”, med andra ord kan komponenterna nås, modifieras och itereras över med hjälp av Pythons standardkonventioner. Du kan till exempel iterera över koordinaterna i en Point:

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

För alla geometriobjekt kan egenskapen GEOSGeometry.coords användas för att få geometrikoordinaterna som en Python-tupel:

Python console
>>> 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 returns a coordinate tuple:

Python console
>>> 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)

Medan indexering på en Polygon kommer att returnera ringen (ett LinearRing-objekt) som motsvarar indexet:

Python console
>>> 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)

Dessutom kan koordinater/komponenter i geometrin läggas till eller ändras, precis som i en Python-lista:

Python console
>>> 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))

Geometrier stöder set-liknande operatorer:

Python console
>>> 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))

GeometriobjektLink to this heading

GEOSGeometriLink to this heading

class GEOSGeometry(geo_input, srid=None, *, max_geom_collections=198)Link to this definition
Parametrar:
  • geo_input – Inmatningsvärde för geometri (sträng eller memoryview)

  • srid (int) – identifierare för rumslig referens

  • 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 object).

Parametern srid, om den anges, sätts som SRID för den skapade geometrin om geo_input inte har någon SRID. Om olika SRIDs tillhandahålls genom parametrarna geo_input och srid, uppstår ValueError:

Python console
>>> 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.

Följande indataformat, tillsammans med motsvarande Python-typer, accepteras:

Format

Inmatning Typ

WKT / EWKT

str

HEX / HEXEWKB

str

WKB / EWKB

minnesvy

GeoJSON

str

För GeoJSON-formatet ställs SRID in baserat på medlemmen crs. Om crs inte anges är standardvärdet för SRID 4326.

classmethod GEOSGeometry.from_gml(gml_string)Link to this definition

Konstruerar en GEOSGeometry från den givna GML-strängen.

EgenskaperLink to this heading

GEOSGeometry.coordsLink to this definition

Returnerar geometrins koordinater som en tupel.

GEOSGeometry.dimsLink to this definition

Returnerar geometrins dimension:

  • 0 för Point` och MultiPoint`

  • 1 för LineString och MultiLineString`

  • 2 för Polygon` och MultiPolygon`

  • -1 för tom :class:`GeometryCollection``s

  • den maximala dimensionen av dess element för icke-tomma :class:`GeometryCollection`s

GEOSGeometry.emptyLink to this definition

Returnerar om uppsättningen punkter i geometrin är tom eller inte.

GEOSGeometry.geom_typeLink to this definition

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

Python console
>>> pnt = GEOSGeometry("POINT(5 23)")
>>> pnt.geom_type
'Point'
GEOSGeometry.geom_typeidLink to this definition

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

GEOSGeometry.num_coordsLink to this definition

Returnerar antalet koordinater i geometrin.

GEOSGeometry.num_geomLink to this definition

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

GEOSGeometry.haszLink to this definition

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

GEOSGeometry.hasmLink to this definition

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

GEOSGeometry.ringLink to this definition

Returnerar en boolean som anger om geometrin är en LinearRing.

GEOSGeometry.simpleLink to this definition

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 object is not simple if it intersects itself. Thus, LinearRing and Polygon objects are always simple because they cannot intersect themselves, by definition.

GEOSGeometry.validLink to this definition

Returnerar ett boolean som anger om geometrin är giltig.

GEOSGeometry.valid_reasonLink to this definition

Returnerar en sträng som beskriver orsaken till att en geometri är ogiltig.

GEOSGeometry.sridLink to this definition

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

Python console
>>> pnt = Point(5, 23)
>>> print(pnt.srid)
None
>>> pnt.srid = 4326
>>> pnt.srid
4326

Egenskaper för utdataLink to this heading

The properties in this section export the GEOSGeometry object into a different. This output may be in the form of a string, buffer, or even another object.

GEOSGeometry.ewktLink to this definition

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).

GEOSGeometry.hexLink to this definition

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 property instead).

GEOSGeometry.hexewkbLink to this definition

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.jsonLink to this definition

Returnerar GeoJSON-representationen av geometrin. Observera att resultatet inte är en komplett GeoJSON-struktur utan endast nyckelinnehållet geometry i en GeoJSON-struktur. Se även GeoJSON Serializer.

GEOSGeometry.geojsonLink to this definition

Alias för GEOSGeometry.json.

GEOSGeometry.kmlLink to this definition

Returns a KML (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.ogrLink to this definition

Returnerar ett OGRGeometry-objekt som motsvarar GEOS-geometrin.

GEOSGeometry.wkbLink to this definition

Returns the WKB (Well-Known Binary) representation of this Geometry as a Python buffer. SRID value is not included, use the GEOSGeometry.ewkb property instead.

GEOSGeometry.ewkbLink to this definition

Returnerar EWKB-representationen av denna geometri som en Python-buffert. Detta är en utvidgning av WKB-specifikationen som inkluderar alla SRID-värden som är en del av denna geometri.

GEOSGeometry.wktLink to this definition

Returnerar geometrins Well-Known Text (en OGC-standard).

Rumsliga predikatmetoderLink to this heading

Alla följande metoder för rumsliga predikat tar en annan GEOSGeometry-instans (other) som parameter och returnerar ett boolean.

GEOSGeometry.contains(other)Link to this definition

Returnerar True om other.within(this) returnerar True.

GEOSGeometry.covers(other)Link to this definition

Returnerar True om denna geometri täcker den angivna geometrin.

Predikatet covers har följande likvärdiga definitioner:

  • Varje punkt i den andra geometrin är en punkt i den här geometrin.

  • Skärningsmatrisen DE-9IM för de två geometrierna är T*****FF*, *T****FF*, ***T**FF*, eller ****T*FF*.

Om någon av geometrierna är tom returneras False.

Detta predikat liknar GEOSGeometry.contains(), men är mer inkluderande (d.v.s. returnerar True för fler fall). I synnerhet, till skillnad från contains() skiljer det inte mellan punkter i geometrins gräns och i dess inre. För de flesta situationer bör covers() föredras framför contains(). Som en extra fördel är covers() mer mottaglig för optimering och bör därför överträffa contains().

GEOSGeometry.crosses(other)Link to this definition

Returnerar True om DE-9IM-intersektionsmatrisen för de två geometrierna är T*T****** (för en punkt och en kurva, en punkt och en yta eller en linje och en yta) 0******** (för två kurvor).

GEOSGeometry.disjoint(other)Link to this definition

Returnerar True om DE-9IM-intersektionsmatrisen för de två geometrierna är FF*FF****.

GEOSGeometry.equals(other)Link to this definition

Returnerar True om DE-9IM-intersektionsmatrisen för de två geometrierna är T*F**FFF*.

GEOSGeometry.equals_exact(other, tolerance=0)Link to this definition

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)Link to this definition

Returnerar True om de två geometrierna är punktvis ekvivalenta genom att kontrollera att strukturen, ordningsföljden och värdena för alla hörn är identiska i alla dimensioner. NaN-värden anses vara lika med andra NaN-värden. Kräver GEOS 3.12.

GEOSGeometry.intersects(other)Link to this definition

Returnerar True om GEOSGeometry.disjoint() är False.

GEOSGeometry.overlaps(other)Link to this definition

Returnerar true om DE-9IM-intersektionsmatrisen för de två geometrierna är T*T***T** (för två punkter eller två ytor) 1*T***T** (för två kurvor).

GEOSGeometry.relate_pattern(other, pattern)Link to this definition

Returnerar True om elementen i DE-9IM-intersektionsmatrisen för denna geometri och den andra matchar det givna mönstret - en sträng med nio tecken från alfabetet: {T, F, *, 0}.

GEOSGeometry.touches(other)Link to this definition

Returnerar True om DE-9IM-intersektionsmatrisen för de två geometrierna är FT*******, F**T***** eller F***T****.

GEOSGeometry.within(other)Link to this definition

Returnerar True om DE-9IM-intersektionsmatrisen för de två geometrierna är T*F**F***.

Topologiska metoderLink to this heading

GEOSGeometry.buffer(width, quadsegs=8)Link to this definition

Returnerar en GEOSGeometry som representerar alla punkter vars avstånd från denna geometri är mindre än eller lika med den givna width. Det valfria nyckelordet quadsegs anger antalet segment som används för att approximera en kvartscirkel (standard är 8).

GEOSGeometry.buffer_with_style(width, quadsegs=8, end_cap_style=1, join_style=1, mitre_limit=5.0)Link to this definition

Samma som buffer(), men gör det möjligt att anpassa buffertens stil.

  • end_cap_style kan vara rund (1), platt (2) eller fyrkantig (3).

  • join_style kan vara rund (1), gering (2) eller fasad (3).

  • Begränsning av mitterförhållande (mitre_limit) påverkar endast mitterfogningsstilen.

GEOSGeometry.difference(other)Link to this definition

Returnerar en GEOSGeometry som representerar de punkter som utgör denna geometri och som inte utgör någon annan.

GEOSGeometry.interpolate(distance)Link to this definition
GEOSGeometry.interpolate_normalized(distance)Link to this definition

Givet ett avstånd (float), returnerar punkten (eller närmaste punkten) inom geometrin (LineString eller MultiLineString) på det avståndet. Den normaliserade versionen tar avståndet som en float mellan 0 (ursprung) och 1 (slutpunkt).

Omvänd av GEOSGeometry.project().

GEOSGeometry.intersection(other)Link to this definition

Returnerar en GEOSGeometry som representerar de punkter som delas av denna geometri och andra.

GEOSGeometry.project(point)Link to this definition
GEOSGeometry.project_normalized(point)Link to this definition

Returnerar avståndet (float) från geometrins ursprung (LineString eller MultiLineString) till den punkt som projiceras på geometrin (dvs. till den punkt på linjen som ligger närmast den angivna punkten). Den normaliserade versionen returnerar avståndet som en flottör mellan 0 (ursprung) och 1 (slutpunkt).

Omvänd av GEOSGeometry.interpolate().

GEOSGeometry.relate(other)Link to this definition

Returnerar DE-9IM-intersektionsmatrisen (en sträng) som representerar det topologiska förhållandet mellan den här geometrin och den andra.

GEOSGeometry.simplify(tolerance=0.0, preserve_topology=False)Link to this definition

Returnerar en ny GEOSGeometry, förenklad till den angivna toleransen med hjälp av Douglas-Peucker-algoritmen. Ett högre toleransvärde innebär färre punkter i utdata. Om ingen tolerans anges är standardvärdet 0.

Som standard bevarar denna funktion inte topologin. Exempelvis kan Polygon-objekt delas upp, kollapsas till linjer eller försvinna. Polygon-hål kan skapas eller försvinna och linjer kan korsas. Genom att ange preserve_topology=True kommer resultatet att ha samma dimension och antal komponenter som indata; detta är dock betydligt långsammare.

GEOSGeometry.sym_difference(other)Link to this definition

Returnerar en GEOSGeometry som kombinerar punkterna i den här geometrin som inte finns i den andra, och punkterna i den andra som inte finns i den här geometrin.

GEOSGeometry.union(other)Link to this definition

Returnerar en GEOSGeometry som representerar alla punkter i den här geometrin och den andra.

Topologiska egenskaperLink to this heading

GEOSGeometry.boundaryLink to this definition

Returnerar gränsen som ett nyligen allokerat Geometry-objekt.

GEOSGeometry.centroidLink to this definition

Returns a Point object representing the geometric center of the geometry. The point is not guaranteed to be on the interior of the geometry.

GEOSGeometry.convex_hullLink to this definition

Returnerar den minsta Polygon som innehåller alla punkter i geometrin.

GEOSGeometry.envelopeLink to this definition

Returnerar en Polygon som representerar den avgränsande omslutningen av denna geometri. Observera att den också kan returnera en Point om indatageometrin är en punkt.

GEOSGeometry.point_on_surfaceLink to this definition

Beräknar och returnerar en Point som garanterat befinner sig på insidan av denna geometri.

GEOSGeometry.unary_unionLink to this definition

Beräknar sammanslagningen av alla element i denna geometri.

Resultatet följer följande kontrakt:

  • Unioning av en uppsättning :class:`LineString`s har effekten av fullständig noding och upplösning av linework.

  • Union av en uppsättning Polygon kommer alltid att returnera en Polygon eller MultiPolygon geometri (till skillnad från GEOSGeometry.union(), som kan returnera geometrier av lägre dimension om en topologisk kollaps inträffar).

Andra egenskaper och metoderLink to this heading

GEOSGeometry.areaLink to this definition

Denna egenskap returnerar geometrins area.

GEOSGeometry.extentLink to this definition

Den här egenskapen returnerar geometrins utsträckning som en 4-tupel, bestående av (xmin, ymin, xmax, ymax).

GEOSGeometry.clone()Link to this definition

Den här metoden returnerar en GEOSGeometry som är en klon av originalet.

GEOSGeometry.distance(geom)Link to this definition

Returnerar avståndet mellan de närmaste punkterna på denna geometri och den givna geometrin (ett annat GEOSGeometry`-objekt).

GEOSGeometry.lengthLink to this definition

Returnerar längden på denna geometri (t.ex. 0 för en Point, längden på en LineString eller omkretsen på en Polygon).

GEOSGeometry.preparedLink to this definition

Returnerar en GEOS PreparedGeometry för innehållet i denna geometri. PreparedGeometry-objekt är optimerade för operationerna contains, intersects, covers, crosses, disjoint, overlaps, touches och within. Se dokumentationen Förberedda geometrier för mer information.

GEOSGeometry.srsLink to this definition

Returnerar ett SpatialReference-objekt som motsvarar geometrins SRID eller None.

GEOSGeometry.transform(ct, clone=False)Link to this definition

Transformerar geometrin enligt den angivna parametern för koordinattransformation (ct), som kan vara ett heltal SRID, en rumslig referens WKT-sträng, en PROJ-sträng, ett SpatialReference-objekt eller ett CoordTransform-objekt. Som standard transformeras geometrin på plats och ingenting returneras. Men om nyckelordet clone är angivet, modifieras inte geometrin och en transformerad klon av geometrin returneras istället.

GEOSGeometry.make_valid()Link to this definition

Returnerar en giltig GEOSGeometry-ekvivalent, som försöker att inte förlora någon av de ingående hörnen. Om geometrin redan är giltig returneras den orörd. Detta liknar databasfunktionen MakeValid. Kräver GEOS 3.8.

GEOSGeometry.normalize(clone=False)Link to this definition

Konverterar denna geometri till kanonisk form. Om nyckelordet clone är angivet, modifieras inte geometrin och en normaliserad klon av geometrin returneras istället:

Python console
>>> 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)

PunktLink to this heading

class Point(x=None, y=None, z=None, srid=None)Link to this definition

Point-objekt instansieras med argument som representerar punktens komponentkoordinater eller med en enda koordinatsekvens. Till exempel: är följande likvärdiga:

Python console
>>> pnt = Point(5, 23)
>>> pnt = Point([5, 23])

Tomma Point objekt kan instansieras genom att skicka inga argument eller en tom sekvens. Följande är likvärdiga:

Python console
>>> pnt = Point()
>>> pnt = Point([])

LineStringLink to this heading

class LineString(*args, **kwargs)Link to this definition

LineString-objekt instansieras med hjälp av argument som antingen är en sekvens av koordinater eller Point-objekt. Till exempel: är följande likvärdiga:

Python console
>>> ls = LineString((0, 0), (1, 1))
>>> ls = LineString(Point(0, 0), Point(1, 1))

Dessutom kan LineString-objekt också skapas genom att skicka in en enda sekvens av koordinat- eller Point-objekt:

Python console
>>> ls = LineString(((0, 0), (1, 1)))
>>> ls = LineString([Point(0, 0), Point(1, 1)])

Tomma LineString-objekt kan instansieras genom att skicka inga argument eller en tom sekvens. Följande är likvärdiga:

Python console
>>> ls = LineString()
>>> ls = LineString([])
closedLink to this definition

Returnerar om denna LineString är stängd eller inte.

LineärRingLink to this heading

class LinearRing(*args, **kwargs)Link to this definition

LinearRing-objekt konstrueras på exakt samma sätt som LineString`-objekt, men koordinaterna måste vara slutna, med andra ord måste de första koordinaterna vara desamma som de sista koordinaterna. Exempelvis:

Python console
>>> ls = LinearRing((0, 0), (0, 1), (1, 1), (0, 0))

Observera att (0, 0) är den första och sista koordinaten - om de inte var lika skulle ett fel uppstå.

is_counterclockwiseLink to this definition

Returnerar om denna LinearRing är motsols.

PolygonLink to this heading

class Polygon(*args, **kwargs)Link to this definition

Polygon objects may be instantiated by passing in parameters that represent the rings of the polygon. The parameters must either be LinearRing instances, or a sequence that may be used to construct a LinearRing:

Python console
>>> 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)Link to this definition

Returnerar ett polygonobjekt från den angivna bounding-boxen, en 4-tupel bestående av (xmin, ymin, xmax, ymax).

num_interior_ringsLink to this definition

Returnerar antalet inre ringar i denna geometri.

Geometri SamlingarLink to this heading

MultiPointLink to this heading

class MultiPoint(*args, **kwargs)Link to this definition

MultiPoint-objekt kan instansieras genom att skicka in Point-objekt som argument, eller en enda sekvens av Point-objekt:

Python console
>>> mp = MultiPoint(Point(0, 0), Point(1, 1))
>>> mp = MultiPoint((Point(0, 0), Point(1, 1)))

MultiLineStringLink to this heading

class MultiLineString(*args, **kwargs)Link to this definition

MultiLineString-objekt kan instansieras genom att skicka in LineString-objekt som argument, eller en enda sekvens av LineString-objekt:

Python console
>>> ls1 = LineString((0, 0), (1, 1))
>>> ls2 = LineString((2, 2), (3, 3))
>>> mls = MultiLineString(ls1, ls2)
>>> mls = MultiLineString([ls1, ls2])
mergedLink to this definition

Returnerar en LineString som representerar linjesammanslagningen av alla komponenter i denna MultiLineString.

closedLink to this definition

Returnerar True om och endast om alla element är stängda.

MultiPolygonLink to this heading

class MultiPolygon(*args, **kwargs)Link to this definition

MultiPolygon-objekt kan instansieras genom att skicka Polygon-objekt som argument, eller en enda sekvens av Polygon-objekt:

Python console
>>> 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])

GeometryCollectionLink to this heading

class GeometryCollection(*args, **kwargs)Link to this definition

GeometryCollection-objekt kan instansieras genom att skicka in andra GEOSGeometry` som argument, eller en enda sekvens av GEOSGeometry`-objekt:

Python console
>>> 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))

Förberedda geometrierLink to this heading

In order to obtain a prepared geometry, access the 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.

Till exempel:

Python console
>>> 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

FörbereddGeometriLink to this heading

class PreparedGeometryLink to this definition

Alla metoder för PreparedGeometry tar ett other argument, som måste vara en GEOSGeometry-instans.

contains(other)Link to this definition
contains_properly(other)Link to this definition
covers(other)Link to this definition
crosses(other)Link to this definition
disjoint(other)Link to this definition
intersects(other)Link to this definition
overlaps(other)Link to this definition
touches(other)Link to this definition
within(other)Link to this definition

Geometri-fabrikerLink to this heading

fromfile(file_h)Link to this definition
Parametrar:

file_h (a Python file object or a string path to the file) – indatafil som innehåller rumsliga data

Returtyp:

en GEOSGeometry som motsvarar de rumsliga data som finns i filen

Exempel:

Python console
>>> from django.contrib.gis.geos import fromfile
>>> g = fromfile("/home/bob/geom.wkt")
fromstr(string, srid=None)Link to this definition
Parametrar:
  • string (str) – sträng som innehåller rumsliga data

  • srid (int) – identifierare för rumslig referens

Returtyp:

en GEOSGeometry som motsvarar de rumsliga data i strängen

fromstr(string, srid) är likvärdigt med GEOSGeometry(string, srid).

Exempel:

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

I/O-objektLink to this heading

LäsobjektLink to this heading

I/O-klasserna för läsare returnerar en GEOSGeometry-instans från WKB- och/eller WKT-ingången som ges till deras read(geom)-metod.

class WKBReaderLink to this definition

Exempel:

Python console
>>> from django.contrib.gis.geos import WKBReader
>>> wkb_r = WKBReader()
>>> wkb_r.read("0101000000000000000000F03F000000000000F03F")
<Point object at 0x103a88910>
class WKTReaderLink to this definition

Exempel:

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

Författarens objektLink to this heading

All writer objects have a write(geom) method that returns either the WKB or WKT of the given geometry. In addition, 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)Link to this definition

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 documentation for more details about the dim argument.

write(geom)Link to this definition

Returnerar WKB för den angivna geometrin som ett Python-objekt i form av en buffer. Ett exempel:

Python console
>>> 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)Link to this definition

Returns WKB of the geometry in hexadecimal. Example:

Python console
>>> from django.contrib.gis.geos import Point, WKBWriter
>>> pnt = Point(1, 1)
>>> wkb_w = WKBWriter()
>>> wkb_w.write_hex(pnt)
'0101000000000000000000F03F000000000000F03F'
byteorderLink to this definition

Denna egenskap kan ställas in för att ändra byte-ordningen för geometrirepresentationen.

Byteordning Värde

Beskrivning

0

Big Endian (t.ex. kompatibel med RISC-system)

1

Little Endian (t.ex. kompatibel med x86-system)

Exempel:

Python console
>>> 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'
outdimLink to this definition

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 värde

Beskrivning

2

Standard, utdata 2D WKB.

3

Utgång 3D WKB.

Exempel:

Python console
>>> 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'
sridLink to this definition

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

Python console
>>> 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)Link to this definition

Denna klass gör det möjligt att skriva ut WKT-representationen av en geometri. Se attributen WKBWriter.outdim, trim och precision för detaljer om konstruktörens argument.

write(geom)Link to this definition

Returnerar WKT för den givna geometrin. Ett exempel:

Python console
>>> from django.contrib.gis.geos import Point, WKTWriter
>>> pnt = Point(1, 1)
>>> wkt_w = WKTWriter()
>>> wkt_w.write(pnt)
'POINT (1.0000000000000000 1.0000000000000000)'
outdimLink to this definition

Se WKBWriter.outdim.

trimLink to this definition

Denna egenskap används för att aktivera eller inaktivera trimning av onödiga decimaler.

Python console
>>> 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)'
precisionLink to this definition

Denna egenskap styr koordinaternas avrundningsprecision; om den är inställd på ”Ingen” är avrundning inaktiverad.

Code
>>> 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)'

Fotnoter

InställningarLink to this heading

GEOS_BIBLIOTEK_ SÖKVÄGLink to this heading

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).

UndantagLink to this heading

exception GEOSExceptionLink to this definition

Base GEOS exception, indikerar ett GEOS-relaterat fel.