search
HomeBackend DevelopmentPython Tutorialpython gdal tutorial: geometry and projection

python gdal tutorial: geometry and projection

Dec 24, 2016 pm 04:01 PM
gdalgeometry

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(, , []) 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(, )

•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
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool