logo资料库

python QGIS二次开发说明文档.pdf

第1页 / 共894页
第2页 / 共894页
第3页 / 共894页
第4页 / 共894页
第5页 / 共894页
第6页 / 共894页
第7页 / 共894页
第8页 / 共894页
资料共894页,剩余部分请下载后查看
QGIS Python Programming Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Automating QGIS
Introduction
Installing QGIS 2.18 for development
Getting ready
How to do it...
Installing PyQGIS using the Debian package manager
Installing PyQGIS using the RPM package manager
Setting the environment variables
Setting the environment variables on Windows
Setting environment variables on Linux
How it works...
There's more...
Finding the PyQGIS path on Windows
Finding the location of the QGIS Python installation on other platforms
Using the QGIS Python console for interactive control
How to do it...
How it works...
Automatically starting the Python console
Getting ready
How to do it...
How it works...
Using the Python's Script Runner plugin
Getting ready
How to do it...
How it works...
There's more...
Setting up your QGIS IDE
Getting ready
How to do it...
Adding the Python interpreter to Windows
Adding the PyQGIS module paths to the interpreter
Adding the PyQGIS API to the IDE
Adding environment variables
How it works...
Debugging QGIS Python scripts
How to do it...
Configuring QGIS
Configuring Eclipse
Testing the debugger
How it works...
Navigating the PyQGIS API
Getting ready
How to do it...
How it works...
There's more...
Creating a traditional QGIS plugin
Getting ready
How to do it...
How it works...
There's more...
Creating a Processing Toolbox plugin
Getting ready
How to do it...
How it works...
There's more...
Distributing a plugin
Getting ready
How to do it...
How it works...
Building a standalone application
Getting ready
How to do it...
How it works...
There's more...
Storing and reading global preferences
Getting ready
How to do it...
How it works...
There's more...
Storing and reading project preferences
Getting ready
How to do it...
How it works...
There's more...
Accessing the script path from within your script
Getting ready
How to do it...
How it works...
There's more...
2. Querying Vector Data
Introduction
Loading a vector layer from a file sample
Getting ready
How to do it...
How it works...
Loading a vector layer from a geodatabase
Getting ready
How to do it...
How it works...
Examining vector layer features
Getting ready
How to do it...
How it works...
Examining vector layer attributes
Getting ready
How to do it...
How it works...
There's more...
Filtering a layer by geometry
Getting ready
How to do it...
How it works...
Filtering a layer by attributes
Getting ready
How to do it...
How it works...
Buffering a feature
Getting ready
How to do it...
How it works...
Measuring the distance between two points
Getting ready
How to do it...
How it works...
Measuring distance along a line
Getting ready
How to do it...
How it works...
Calculating the area of a polygon
Getting ready
How to do it...
How it works...
Creating a spatial index
Getting ready
How to do it...
How it works...
Calculating the bearing of a line
Getting ready
How to do it...
How it works...
Loading data from a spreadsheet
Getting ready
How to do it...
How it works...
There's more...
Accessing layer metadata
Getting ready
How to do it...
How it works...
3. Editing Vector Data
Introduction
Creating a vector layer in memory
Getting ready
How to do it...
How it works...
Adding a point feature to a vector layer
Getting ready
How to do it...
How it works...
There's more...
Adding a line feature to a vector layer
Getting ready
How to do it...
How it works...
Adding a polygon feature to a vector layer
Getting ready
How to do it...
How it works...
Adding a set of attributes to a vector layer
Getting ready...
How to do it...
How it works...
Adding a field to a vector layer
Getting ready
How to do it...
How it works...
Joining a shapefile attribute table to a CSV file
Getting ready
How to do it...
How it works...
There's more...
Changing vector layer geometry
Getting ready
How to do it...
How it works...
Changing a vector layer feature's attribute
Getting ready
How to do it...
How it works...
Removing data from a vector layer
Getting ready
How to do it....
How it works...
Deleting a vector layer feature's attribute
Getting ready
How to do it...
How it works...
Reprojecting a vector layer
Getting ready
How to do it...
How it works...
Converting a shapefile to KML or GeoJSON
Getting ready
How to do it...
How it works...
There's more...
Merging shapefiles
Getting ready
How to do it...
How it works...
There's more...
Splitting a shapefile
Getting ready
How to do it...
How it works...
Generalizing a vector layer
Getting ready
How to do it...
How it works...
Dissolving vector features
Getting ready
How to do it...
How it works...
Performing a union on vector shapes
Getting ready
How to do it...
How it works...
Rasterizing a vector layer
Getting ready
How to do it...
How it works...
Exporting a layer to the GeoPackage format
Getting ready
How to do it...
How it works...
4. Using Raster Data
Introduction
Loading a raster layer
Getting ready
How to do it...
How it works...
Loading a NetCDF file
Getting ready
How to do it...
How it works...
Getting the cell size of a raster layer
Getting ready
How to do it...
How it works...
Obtaining the width and height of a raster
Getting ready
How to do it...
How it works...
Counting raster bands
Getting ready
How to do it...
How it works...
Swapping raster bands
Getting ready
How to do it...
How it works...
There's more...
Querying the value of a raster at a specified point
Getting ready
How to do it...
How it works...
Reprojecting a raster
Getting ready
How to do it...
How it works...
There's more...
Creating an elevation hillshade
Getting ready
How to do it...
How it works...
There's more...
Creating vector contours from elevation data
Getting ready
How to do it...
How it works...
Sampling a raster dataset using a regular grid
Getting ready
How to do it...
How it works...
There's more...
Adding elevation data to a line vertices using a digital elevation model
Getting ready
How to do it...
How it works...
There's more...
Creating a common extent for rasters
Getting ready
How to do it...
How it works...
Resampling raster resolution
Getting ready
How to do it...
How it works...
Counting the unique values in a raster
Getting ready
How to do it...
How it works...
Mosaicing rasters
Getting ready
How to do it...
How it works...
Converting a TIFF image to a JPEG image
Getting ready
How to do it...
How it works...
Creating pyramids for a raster
Getting ready
How to do it...
How it works...
Converting a pixel location to a map coordinate
Getting ready
How to do it...
How it works...
Converting a map coordinate to a pixel location
Getting ready
How to do it...
How it works...
Creating a KML image overlay for a raster
Getting ready
How to do it...
How it works...
There's more...
Classifying a raster
Getting ready
How to do it...
How it works...
Converting a raster to a vector
Getting ready
How to do it...
How it works...
Georeferencing a raster from control points
Getting ready
How to do it...
How it works...
Clipping a raster using a shapefile
Getting ready
How to do it...
How it works...
5. Creating Dynamic Maps
Introduction
Accessing the map canvas
Getting ready
How to do it...
How it works...
Changing the map units
Getting ready
How to do it...
How it works...
Iterating over layers
Getting ready
How to do it...
How it works...
Symbolizing a vector layer
Getting ready
How to do it...
How it works...
Setting a transparent layer fill
Getting ready
How to do it...
How it works...
Using a filled marker symbol
Getting ready
How to do it...
Rendering a single band raster using a color ramp algorithm
Getting ready
How to do it...
How it works...
Setting a feature's color using a column in a CSV file
Getting ready
How to do it...
How it works...
Creating a complex vector layer symbol
Getting ready
How to do it...
How it works...
Using icons as vector layer symbols
Getting ready
How to do it...
How it works...
Using an outline for font markers
Getting ready
How to do it...
How it works...
Using arrow symbols
Getting ready
How to do it...
How it works...
Creating a graduated vector layer symbol renderer
Getting ready
How to do it...
How it works...
Creating a categorized vector layer symbol
Getting ready
How to do it...
How it works...
Using live layer effects
Getting ready
How to do it...
How it works...
Creating inverted polygon shapeburst fills
Getting ready
How to do it...
How it works...
Creating a map bookmark
Getting ready
How to do it...
How it works...
Navigating to a map bookmark
Getting ready
How to do it...
How it works...
Setting scale-based visibility for a layer
Getting ready
How to do it...
How it works...
Using SVG for layer symbols
Getting ready
How to do it...
How it works...
Using pie charts for symbols
Getting ready
How to do it...
How it works...
There's more...
Using the 2.5D renderer
Getting ready
How to do it...
How it works...
Using XYZ tiled map services
Getting ready
How to do it...
How it works...
Using Esri ArcGIS map services
Getting Ready
How to do it...
How it works...
Labeling a feature
Getting ready
How to do it...
How it works...
Using null symbols
Getting ready
How to do it...
How it works...
Change map layer transparency
Getting ready
How to do it...
How it works...
Adding standard map tools to the canvas
Getting ready
How to do it...
How it works...
Using a map tool to draw points on the canvas
Getting ready
How to do it...
How it works...
Using a map tool to draw polygons or lines on the canvas
Getting ready
How to do it...
How it works...
Building a custom selection tool
Getting ready
How to do it...
How it works...
Creating a mouse coordinate tracking tool
Getting ready
How to do it...
How it works...
6. Composing Static Maps
Introduction
Creating the simplest map renderer
Getting ready
How to do it...
How it works...
There's more...
Using the Map Composer
Getting ready
How to do it...
How it works...
There's more...
Adding labels to a map for printing
Getting ready
How to do it...
How it works...
Adding a scale bar to a map
Getting ready
How to do it...
How it works...
Adding a North arrow to the map
Getting ready
How to do it...
How it works...
There's more...
Adding a logo to the map
Getting ready
How to do it...
How it works...
Adding a vertical legend to the map
Getting ready
How to do it...
How it works...
Adding a horizontal legend
Getting ready
How to do it...
Adding a custom shape to the map
Getting ready
How to do it...
How it works...
There's more...
Adding a grid to the map
Getting ready
How to do it...
How it works...
Adding a table to the map
Getting ready
How to do it...
How it works...
Adding a world file to a map image
Getting ready
How to do it...
How it works...
Saving a map to a project
Getting ready
How to do it...
How it works...
Loading a map from a project
Getting ready
How to do it...
How it works...
7. Interacting with the User
Introduction
Using log files
Getting ready
How to do it...
How it works...
There's more...
Creating a simple message dialog
Getting ready
How to do it...
How it works...
There's more...
Creating a warning dialog
Getting ready
How to do it...
How it works...
Creating an error dialog
Getting ready
How to do it...
How it works...
Displaying a progress bar
Getting ready
How to do it...
How it works...
There's more...
Creating a simple text input dialog
Getting ready
How to do it...
How it works...
Creating a file input dialog
Getting ready
How to do it...
How it works...
There's more...
Creating a combobox
Getting ready
How to do it...
How it works...
Creating radio buttons
Getting ready
How to do it...
How it works...
Creating checkboxes
Getting ready
How to do it...
How it works...
Creating a dock widget
Getting ready
How to do it...
How it works...
Displaying a message in the status bar
Getting ready
How to do it...
How it works...
Pushing messages to the message bar
Getting ready
How to do it...
How it works...
Pushing widgets to the message bar
Getting ready
How to do it...
How it works...
Creating tabs
Getting ready
How to do it...
How it works...
Stepping the user through a wizard
Getting ready
How to do it...
How it works...
Keeping dialogs on top
Getting ready
How to do it...
How it works...
8. QGIS Workflows
Introduction
Creating an NDVI
Getting ready
How to do it...
How it works...
Geocoding addresses
Getting ready
How to do it...
How it works...
Creating raster footprints
Getting ready
How to do it...
How it works...
There's more...
Performing network analysis
Getting ready
How to do it...
How it works...
There's more...
Routing along streets
Getting ready
How to do it...
How it works...
Tracking a GPS
Getting ready
How to do it...
How it works...
There's more...
Creating a mapbook
Getting ready
How to do it...
How it works...
Finding the least cost path
Getting ready
How to do it...
How it works...
Performing nearest neighbor analysis
Getting ready
How to do it...
How it works...
Creating a DEM from LIDAR
Getting ready
How to do it...
How it works...
Creating a heat map
Getting ready
How to do it...
How it works...
Creating a dot density map
Getting ready
How to do it...
How it works...
Collecting field data
Getting ready
How to do it...
How it works...
Computing road slope using elevation data
Getting ready
How to do it...
How it works...
Geolocating photos on the map
Getting ready
How to do it...
How it works...
There's more...
Image change detection
Getting ready
How to do it...
How it works...
Adjusting imprecise building footprints
Getting ready
How to do it...
How it works...
Visualizing multitemporal data
Getting ready
How to do it...
How it works...
9. Other Tips and Tricks
Introduction
Creating tiles from a QGIS map
Getting ready
How to do it...
How it works...
There's more...
Adding a layer to geojson.io
Getting ready
How to do it...
How it works...
There's more...
Rendering map layers based on rules
Getting ready
How to do it...
How it works...
Creating a layer-definition file
Getting ready
How to do it...
How it works...
Using NULL values in PyQGIS
Getting ready
How to do it...
How it works...
Using generators for layer queries
Getting ready
How to do it...
How it works...
Using alpha values to show data density
Getting ready
How to do it...
How it works...
Using the __geo_interface__ protocol
Getting ready
How to do it...
How it works...
Getting the output file names from processing algorithms
Getting ready
How to do it...
How it works...
Generating points along a line
Getting ready
How to do it...
How it works...
There's more...
Using expression-based labels
Getting ready
How to do it...
How it works...
Creating dynamic forms in QGIS
Getting ready
How to do it...
How it works...
There's more...
Calculating length for all selected lines
Getting ready
How to do it...
How it works...
Using a different status bar CRS than the map
Getting ready
How to do it...
How it works...
Creating HTML labels in QGIS
Getting ready
How to do it...
How it works...
There's more...
Using OpenStreetMap points of interest in QGIS
Getting ready
How to do it...
How it works...
Changing the QGIS web proxy
Getting ready
How to do it...
How it works...
There's more...
Running QGIS scripts as scheduled tasks
Getting ready
How to do it...
How it works...
Visualizing data in 3D with WebGL
Getting ready
How to do it...
How it works...
Visualizing data on a globe
Getting ready
How to do it...
How it works...
Make a globe-like azimuthal orthographic projection
Getting ready
How to do it...
How it works...
Animating a layer
Getting ready
How to do it...
QGIS Python Programming Cookbook - Second Edition
Table of Contents QGIS Python Programming Cookbook - Second Edition Credits About the Author About the Reviewer www.PacktPub.com Why subscribe? Customer Feedback Preface What this book covers What you need for this book Who this book is for Sections Getting ready How to do it… How it works… There's more… See also Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. Automating QGIS Introduction Installing QGIS 2.18 for development Getting ready How to do it... Installing PyQGIS using the Debian package manager Installing PyQGIS using the RPM package manager Setting the environment variables Setting the environment variables on Windows Setting environment variables on Linux How it works... There's more... How to do it... Finding the PyQGIS path on Windows Finding the location of the QGIS Python installation on other platforms Using the QGIS Python console for interactive control
Automatically starting the Python console Using the Python's Script Runner plugin How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Getting ready How to do it... Setting up your QGIS IDE Adding the Python interpreter to Windows Adding the PyQGIS module paths to the interpreter Adding the PyQGIS API to the IDE Adding environment variables How it works... Debugging QGIS Python scripts How to do it... Configuring QGIS Configuring Eclipse Testing the debugger How it works... Navigating the PyQGIS API Creating a traditional QGIS plugin Creating a Processing Toolbox plugin Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... There's more... Distributing a plugin Getting ready How to do it... How it works... Building a standalone application Getting ready
Storing and reading global preferences Storing and reading project preferences Accessing the script path from within your script 2. Querying Vector Data Introduction Loading a vector layer from a file sample Loading a vector layer from a geodatabase How to do it... How it works... There's more... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Examining vector layer features Examining vector layer attributes Filtering a layer by geometry Filtering a layer by attributes
Measuring the distance between two points Buffering a feature Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Measuring distance along a line Calculating the area of a polygon Creating a spatial index Calculating the bearing of a line Loading data from a spreadsheet Accessing layer metadata Getting ready How to do it... How it works... 3. Editing Vector Data Introduction Creating a vector layer in memory Adding a point feature to a vector layer Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Adding a line feature to a vector layer
Adding a polygon feature to a vector layer Adding a set of attributes to a vector layer Adding a field to a vector layer Joining a shapefile attribute table to a CSV file Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready... How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it.... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... Changing vector layer geometry Changing a vector layer feature's attribute Removing data from a vector layer Deleting a vector layer feature's attribute Reprojecting a vector layer Converting a shapefile to KML or GeoJSON
Merging shapefiles Getting ready How to do it... How it works... There's more... Splitting a shapefile Getting ready How to do it... How it works... Generalizing a vector layer Dissolving vector features Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Performing a union on vector shapes Rasterizing a vector layer Exporting a layer to the GeoPackage format Getting ready How to do it... How it works... 4. Using Raster Data Introduction Loading a raster layer Loading a NetCDF file Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready Getting the cell size of a raster layer Obtaining the width and height of a raster
分享到:
收藏