I wondered if it is possible to look at the contents of a shapefile using Python without having and ArcMap license. The situation is that you can create shapefiles from many different applications, not only from ESRI software. I would like to create a Python script that checks the spatial reference, feature type, attribute names and definitions, and the contents of the fields in a shapefile and compares them to a set of acceptable values. I would like this script to work even if the organization does not have any ESRI licenses. To do something like this, do you have to use ArcPy or can you dig into a shapefile without using ArcPy?
View Shapefile Contents Using Python Without ArcMap License – How to Guide
pythonshapefile
Related Solutions
GDAL is the tool to use. In fact that entire call is one line for gdal_rasterize:
gdal_rasterize -l mask -i -burn -9999 mask.shp elevation.tif
if you knew the no data value of the dem
For some python control:
lyr = 'mask'
shp = 'mask.shp'
dem = 'elevation.tif'
ndv = -9999
p = os.Popen('gdal_rasterize -l %s -i -burn %d %s %s' % (lyr,ndv,shp,dem)
where your variables could be set in python
For full python:
from osgeo import gdal, ogr
from osgeo.gdalconst import *
shp = ogr.Open('mask.shp')
lyr = shp.GetLayer('mask')
dem = gdal.Open('elevation.tif', GA_Update)
ndv = dem.GetRasterBand(1).GetNoDataValue()
gdal.RasterizeLayer(dem, 1, lyr, None, ndv) # other options, such as transformer func, creation options...
dem = None
I just took a quick peek at the syntax for the C API, so my syntax for python is probably off a little. See gdal_alg.h: http://gdal.org/gdal__alg_8h.html
I do not know ArcPy, but I work with shapefiles and raster in Python for years
- For processing shapefiles in Python, there are many modules like osgeo/ogr, Fiona, Pysal or Pyshp (shpUtils is one of them and not the most used), and others, see Pypi: GIS and examples on gis.stackexchange and many examples on the Web (not only in English). Most of them are much older than ArcPy (or arcgisscripting)...
- for processing raster you can use osgeo/gdal, the standard
- For processing geospatial geometries, there is shapely
- For plotting the geometries you can use matplotlib and possibly descartes, "extension" of matplotlib for areas, but also many, many other modules, see Pypi: Plotting and modules like mayavi for 3D representation (matplotlib also)
- There are also modules like mapnik which give you directly the possibilities of 1) read a shapefile and 4) plotting with the module Pycairo.
After that, it's like a GIS:
- you use the modules 1) to open, save the shapefiles and carry out the treatments with other modules like numpy or scipy, if you want.
- you can use shapely for manipulation and analysis of the geometric objects (buffer, etc.).
- you can use matplotlib to plot the geometries, but matplotlib do not know what you want to plot. It is your work with modules 1) or 3) to specify what to plot (attributes, etc,.) and how.
If I want to visualise one certain column of my shapefile, how can I implement this in the code?
So, you must learn matplotib and the other modules. You have to learn ArcPy, it's the same...(there are lots of excellent tutorials on the web, especially for matplolib, and it's easier that ArcPy because it is pure Python).
Some examples with Python only
Geological map (polygon shapefile) with colors based on an attribute
3D Points (PointZ shapefile) with color based on an attribute
3D points (Point shapefile with z as attribute) and 3D line (PolyLineZ shapefile) on a DEM, and on a raster draped onto the DEM surface.
Topographic profile with z values and colors based on attributes (geological formations = Cross section) of the original shapefile (Polyline shapefile)
DEM (GeoTIFF) with the module Mayavi2
DEM (ESRI ascii grid, .asc) and Point shapefiles (with z as attribute) with the module visvis
Boreholes (3D buffer of a polylineZ with colors based on an attribute (geological formations), with a grid surface calculated with the modules numpy and matplotlib from a points shapefile (with z as an attribute), visualized with the module visvis
Best Answer
I would recommend becoming familiar with the Python GDAL/OGR API to work with both vector and raster data. The easiest way to start using GDAL/OGR is via a python distribution such as python(x,y), Anaconda, or OSGeo4W.
Further details on using GDAL for your specific tasks:
Additionally, I would recommend the following tutorial from USU to get you started.
Borrowing from the examples above, the following script uses FOSS tools to perform the following actions: