Home Backend Development Python Tutorial python gdal tutorial: geometry and projection

python gdal tutorial: geometry and projection

Dec 24, 2016 pm 04:01 PM
gdal geometry

Create an empty geometry object: ogr.Geometry

The methods used to define various geometries are different (point, line, polygon, etc)

To create a new point, use the method AddPoint( , < ;y>, []). The z coordinate is generally omitted, and the default value is 0

For example:

point = ogr.Geometry(ogr.wkbPoint)

point.AddPoint(10,20)

New line

Use AddPoint(< x>, , []) to add a point

Use SetPoint(, , , []) to change the coordinates of a point

For example, the following code changes the coordinates of point 0:

line = ogr.Geometry(ogr.wkbLineString)

line.AddPoint(10,10)

line.AddPoint(20,20)

line. SetPoint(0,30,30) #(10,10) -> (30,30)

Count the number of all points

print line.GetPointCount()

Read the x coordinate and y coordinate of point 0

print line.GetX(0)

print line.GetY(0)

To create a new polygon, first create a new ring, and then add the ring to the polygon object.

How to create a ring? First create a new ring object, and then add points to it one by one.

ring = ogr.Geometry(ogr.wkbLinearRing)

ring.AddPoint(0,0)

ring.AddPoint(100,0)

ring.AddPoint(100,100)

ring.AddPoint(0,100)

At the end, use CloseRings to close the ring, or set the coordinates of the last point to be the same as the first point.

ring.CloseRings()

ring.AddPoint(0,0)

The following is an example to create a box. This is a polygon object, composed of two layers of rings. Outring = OGR.Geometry (OGR.WKBLINEARING)

Outring.addpoint (0,0). ) outring.AddPoint(0,0)

inring = ogr.Geometry(ogr.wkbLinearRing)inring = ogr.Geometry(ogr.wkbLinearRing)

inring.AddPoint(25,25)

inring.AddPoint(75,25)

inring.AddPoint(75,75)

inring.AddPoint(25,75)

inring.CloseRings()

polygon = ogr.Geometry(ogr.wkbPolygon)

polygon.AddGeometry(outring)

polygon .AddGeometry(inring)

The last three sentences are more important, that is, first create a polygon object, and then add the outer ring and inner ring

The following sentence can help you count how many rings your polygon can have

print polygon.GetGeometryCount()

Read ring from polygon, the order of index is the same as the order of adding ring when creating polygon

outring = polygon.GetGeometryRef(0)

inring = polygon.GetGeometryRef(1)

Create multi geometry

such as MultiPoint, MultiLineString, MultiPolygon

Use AddGeometry to add ordinary geometric shapes to the composite geometry, for example:

multipoint = ogr.Geometry(ogr.wkbMultiPoint)

point = ogr. Geometry(ogr.wkbPoint)point = ogr.Geometry(ogr.wkbPoint)

point.AddPoint(10,10)

multipoint.AddGeometry(point)

point.AddPoint(20,20)

multipoint.AddGeometry( point)

Reading Geometry in MultiGeometry is the same as reading ring from Polygon. It can be said that Polygon is a built-in MultiGeometry.

Don’t delete an existing Feature’s Geometry, it will crash python.

You can only delete the Geometry created during the running of the script, for example, manually created, or automatically created by calling other functions. Even if this Geometry has been used to create other Features, you can still delete it.

For example: Polygon.Destroy()

Regarding projection Projections, use SpatialReference object

A variety of Projections, GDAL supports WKT, PROJ.4, ESPG, USGS, ESRI.prj

can be read from layer and Geometry Get Projections, for example:

spatialRef = layer.GetSpatialRef()

spatialRef = geom.GetSpatialReference()

Projection information is generally stored in the .prj file. If there is no such file, the above function returns None

Create a new one Projection:

First import the osr library, then use osr.SpatialReference() to create a SpatialReference object

Then use the following statements to import the projection information to the SpatialReference object

  •ImportFromWkt()

  •ImportFromEPSG( ; ;)

 •ImportFromUSGS(< proj_code>, )

•ImportFromXML()

Export Projection, use the following statement to export it as a string

​ •ExportToWkt()

​ •ExportToPrettyWkt()

​ •ExportToPro​ j4()

  •ExportToPCI()

  •ExportToUSGS()

•ExportToXML()

To perform projection transformation on a geometric shape Geometry, you must first initialize two Projections, then create a CoordinateTransformation object and use it for transformation

sourceSR = osr.SpatialReference()

sourceSR.ImportFromEPSG(32612) #UTM 12N WGS84

targetSR = osr.SpatialReference()

targetSR.ImportFromEPSG(4326) #Geo WGS84

coordTrans = osr.CoordinateTransformation(sourceSR, targetSR)

geom.Transform(coordTrans)

But this code is very annoying ! It doesn't work in windows. There is a discussion in the foreigner's forum, saying that there is no problem in Linux, but Windows is dead, hehe. . .

There are a few more things to pay attention to:

Edit the Geometry at the appropriate time. It is best not to move it after the projection transformation.

To perform projection transformation on all Geometry in a DataSource, you have to do it one by one. Use a loop.

Writing your projection into a .prj file is actually very simple. First, MorphToESRI(), convert it into a string, then open a text file and write it in it. For example:

targetSR.MorphToESRI()

file = open('test.prj', 'w')

file.write(targetSR.ExportToWkt())

ffile.close()

The above is python gdal Tutorial: Geometry and projection content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles