Is there a Python module that can contour data without gridding - python

I am trying to contour a set of irregularly spaced data (2D contours in this case) without first gridding the data. The data are output from a model and I want the contours to fully honor the underlying data. Is there a Python module with this functionality, perhaps using Delaunay triangulation? Ideally I could export the contours as a shapefile, but could probably work with a matplotlib plot or similar. Also open to other open-source approaches if I can't figure it out in Python.
Thanks

Have a look #
Shapely https://github.com/sgillies/shapely
and
scipy Delaunay¶ http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html

Related

contour plot in bokeh

I would like to use pyplot.contour feature in bokeh. Is there any way I can use it?? I know pyplot.pcolormesh and bokeh.plotting.image. Can I use conotur plot with it?
pyplot.contour is part of Matplotlib, not Bokeh. As of Bokeh 2.3.0 there is no built-in contouring function or capability. Recently a MultiPolygons glyph that can support "polygons with holes" was added. This is a first necessary step to being able to have real contour plots in Bokeh. A next step would be for someone to write a set of functions that can accept array inputs and generate the multi-polygon data necessary to drive Bokeh graphics, but this has not been done by anyone yet.
If image contour plots (similar to pcolormesh) or line (unfilled) contours suit your needs, that you can consider using Holoviews, which can generate Bokeh contour plots for those kinds of cases:
http://holoviews.org/reference/elements/bokeh/Contours.html

Could anyone please show me how to correctly use GDAL Grid in Python?

I am trying to create a group of contour polygons from a set of scatter points containing numeric data of some sorts, for example temperature.
The method I used to apply is to generate a raster through spatial interpolation then perform contour then construct polygons with these contour lines or convert raster cells to polygons. All these were done in ArcGIS.
But considering the expiration of license, I want to do the same thing in plain python. I tried PostGIS but it did not work out. Now I turn to GDAL.
My plan is to create a smooth surface geotiff file from points with gdal.Grid, then use gdal.ContourGenerate to get contour lines, and finally construct polygons with these contour lines and some mask line rings such as a border. However I am stuck at the very first step.
This is my code, the script only creates empty geotiff file, the documentation of GDAL does not really tell much on this function. Could anyone help me solve this?
the input test.shp is a ESRI shapefile, it has a column named 'value' which stores the data I want to use for interpolation.
import gdal
option = gdal.GridOptions(format='GTiff',
width=250,height=250,
algorithm='invdist:power=2',
layers=['d:/work/contour/test.shp'],
zfield='value')
out = gdal.Grid('d:/work/contour/test.tif',
'd:/work/contour/test.shp',
options=option)
out.FlushCache()
out = None
del out

'Crop' basemap (Python) projection to cover only 1 hemisphere

I'm working with the Hammer projection defined in basemap (I use basemap's version instead on the one defined directly in maptplotlib due to the ability to change the lon_0 parameter).
But my goal is to represent sky maps generated from ground, so it doesn't make sense to plot the southern hemisphere of the map (ground).
https://dl.dropboxusercontent.com/u/66372761/skymap.png
What I would like (more or less)
https://dl.dropboxusercontent.com/u/66372761/skymap_crop.png
Is there any way to achieve this with this basemap module?. One option would be of course to save the image and then crop it with p.e. imagemagick, but this seems a bit ugly workaround, and the results would be far from perfect due to the axis labels and so.
I see that someone managed to get something similar with the custom projection of matplotlib, matplotlib: custom projection for hemisphere/wedge, but it's with the standard matplotlib, not with the basemap module.
PS. I'm using Python 2.7, matplotlib 1.3.1 and basemap 1.0.7.
Thanks in advance,
Miguel
Ordinarily, the way to show only part of the map is using the height, width, lat_0, and lon_0 parameters in the Basemap() constructor. However, the hammer projection ignores all but the lon_0 parameter, meaning the only way to crop the image in the way you want is to crop the image after it's created by basemap or use a different type of projection.

How to do zonal statistics on raster file based on polyon in Python?

I have a raster file and a polygon shapefile. I like to get the mean of the raster file of the area the polygon covers. I like to do this in a python standalone script. So QGIS and Starspan do not work. Also Arcpy is not available. I like to use GDAL. What Python package? ways to do this can you recommend?
There is a gdal.RasterizeLayer function which let you rasterize a vector layer. It has some downsides, you need to have an output dataset to which you rasterize. In addition, if you have overlapping geometries, you want to first isolate each geometry on a seperate vector layer, meaning you have to loop over all geometries.
With gdal you can create in-memory files by using the MEM driver, this makes it a bit easier, but there is still a lot of dataset-creation overhead.
For each geometry, the steps would be more or less like:
driver = gdal.GetDriverByName('MEM')
outds = driver.Create('', pixelxsize, pixelysize, 1, GDT_Byte)
outds.SetProjection(target_proj)
outds.SetGeoTransform(target_gt)
gdal.RasterizeLayer(outds, [1], vectorlayer, burn_values=[1])
Now the outds contains a mask of the geometry, using it with for example np.masked_where you can isolate the pixels within the geometry.
Its not as convenient as it good be, but once you have a masked array of the polygon, its very easy to get statistics by using numpy/scipy.
edit:
See this script for some more detailed examples:
http://svn.osgeo.org/gdal/trunk/autotest/alg/rasterize.py
This is way late, but there's an explicit python package for this question now, xagg, which you can install with
conda install xagg.
regionmask is also an option - it can provide masks of polygons (which can be used in a .where().mean() call using xarray), but does not area-average using the overlap between grid cells and polygons, so can be less accurate when the raster data has a low resolution.

Draw a map with gps markers and save this image to file

I have some GPS position recordings, which I want to draw into a map (e.g. OpenStreetMap) and save this as an image file.
I'm using python. I've found osm-gps-map as a candidate library to do the visualization, but apparantly the API lacks some functionality to satisfy all of my needs. What I need is a library that does:
draw stuff into a map (e.g. an OSM-based map), i.e. a list of GPS positions.
automatically adjust view (scale/position) to fit in all drawn positions
save this view to an image file
I seriously want to avoid being forced to manually make a screenshot from a widget or a browser window. And if possible I also want to avoid to implement the necessary projection functionality by myself (as in here). Does anyone know a library/toolchain that provides the desired functionality (if possible for use by Python)?
You may want to consider using Basemap for matplotlib. Here is a blog post describing how to use this package with OSM.
There are examples of how to draw custom GPS points on top of OSM background map using Matplotlib Basemap, Matplotlib or Cairo in GeoTiler project: https://github.com/wrobell/geotiler/tree/master/examples.

Categories

Resources