Exemplo n.º 1
0
def test_basic_test_17_part_2():

    # For some odd reason, this fails on the Travis CI targets after unrelated
    # changes (https://travis-ci.com/github/OSGeo/gdal/jobs/501940381)
    if gdaltest.skip_on_travis():
        pytest.skip()

    from osgeo import ogr

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        flag = False
        try:
            ogr.DontUseExceptions()
            gdal.DontUseExceptions()
            flag = True
        except:
            gdal.DontUseExceptions()
            ogr.DontUseExceptions()
        assert not flag, 'expected failure'
        assert not gdal.GetUseExceptions()
        assert not ogr.GetUseExceptions()
Exemplo n.º 2
0
def ogr_basic_15():

    ds = ogr.Open('data/poly.shp')
    lyr = ds.GetLayer(0)

    used_exceptions_before = ogr.GetUseExceptions()
    ogr.UseExceptions()
    try:
        lyr.CreateFeature(ogr.Feature(lyr.GetLayerDefn()))
    except RuntimeError as e:
        ok = str(e).find(
            'CreateFeature : unsupported operation on a read-only datasource'
        ) >= 0
        if not ok:
            print('Got: %s' + str(e))
            return 'fail'
        return 'success'
    finally:
        if used_exceptions_before == 0:
            ogr.DontUseExceptions()

    print('Expected exception')
    return 'fail'
Exemplo n.º 3
0
# -*- coding: utf-8 -*-
from builtins import str
import shapely
from shapely.geometry import shape, box, MultiPolygon
import numpy as np
from collections import Counter
from osgeo import gdal, ogr
from osgeo.gdalconst import GA_ReadOnly
from .utils import bbox_to_pixel_offsets, shapely_to_ogr_type, get_features, \
                   RasterStatsError, raster_extent_as_bounds

if ogr.GetUseExceptions() != 1:
    ogr.UseExceptions()

DEFAULT_STATS = ['count', 'min', 'max', 'mean']
VALID_STATS = DEFAULT_STATS + \
    ['sum', 'std', 'median', 'all', 'majority', 'minority', 'unique', 'range']

# Correspondance entre python2 "basestring" et python3 "str"
try:
    basestring
except NameError:
    basestring = str


def raster_stats(vectors,
                 raster,
                 layer_num=0,
                 band_num=1,
                 nodata_value=None,
                 global_src_extent=False,
Exemplo n.º 4
0
# geojson.py
#
# ---copyright goes here---
# this is python2 code
# requirements: gdal with its python bindings installed

import os
import osgeo.ogr as ogr

ogr.UseExceptions()  #make ogr closer to sane
assert ogr.GetUseExceptions() == True

#import geojson

#functions we care about:
# ogr.open() (not, not gdal.open, that's for rasters!!)
# layer.TestCapability("FastSpatialFilter")
# layer.GetFeatureCount()
# feature.geometry()
# layer.SetSpatialFilterRect()
# feature['key']

import IPython

IPython.terminal.embed.TerminalInteractiveShell.confirm_exit = False  #i want
#IPython.get_config().InteractiveShell.confirm_exit = False


def features(shapefile):
    "a simple iterator that returns items from a shapefile"
    "precondition: your shapefile has exactly one layer (you can split it up with gdal or qgis if this is not true)"