コード例 #1
0
def load_geometries(directory, good_sources, area):
    ''' Load a set of GeoJSON geometries should be rendered.
    '''
    good_geometries, bad_geometries = list(), list()

    osr.UseExceptions()
    sref_geo = osr.SpatialReference()
    sref_geo.ImportFromProj4(EPSG4326)
    sref_map = osr.SpatialReference()
    sref_map.ImportFromProj4(EPSG2163 if area == USA else ESRI54029)
    project = osr.CoordinateTransformation(sref_geo, sref_map)

    for path in glob(join(directory, '*.json')):
        with open(path) as file:
            data = json.load(file)

        if 'geometry' in data.get('coverage', {}):
            geojson = json.dumps(data['coverage']['geometry'])
            geometry = ogr.CreateGeometryFromJson(geojson)

            if not geometry:
                continue

            geometry.Transform(project)

            if basename(path) in good_sources:
                good_geometries.append(geometry)
            else:
                bad_geometries.append(geometry)

    return good_geometries, bad_geometries
コード例 #2
0
ファイル: draw_regions.py プロジェクト: jiaojiashuang/RPN
def delete_points_in_countries(points_lat_long, points, indices, countries=None,
                               path="data/shp/countries/cntry00.shp"):
    from osgeo import ogr
    from osgeo import osr

    query = "CNTRY_NAME IN (\'" + "\',\'".join(countries) + "\')"
    print(query)
    ogr.UseExceptions()
    osr.UseExceptions()
    driver = ogr.GetDriverByName("ESRI Shapefile")
    dataStore = driver.Open(path, 0)
    layer = dataStore.GetLayer(0)
    layer.SetAttributeFilter(query)

    feature = layer.GetNextFeature()
    while feature:
        print(list(feature.items())["CNTRY_NAME"])
        geom = feature.GetGeometryRef()

        if list(feature.items())["CNTRY_NAME"] != "Russia":
            print(geom.ExportToWkt())

        to_del = filter(lambda x: geom.Distance(x[1]) < 0.2,
                        list(zip(points, points_lat_long, indices)))

        to_del = list(to_del)
        for p_del, p_ll_del, i_del in to_del:
            points.remove(p_del)
            indices.remove(i_del)
            points_lat_long.remove(p_ll_del)
        feature = layer.GetNextFeature()

        pass

    dataStore.Destroy()
コード例 #3
0
ファイル: render.py プロジェクト: stefanb/machine
def load_geometries(directory, good_sources, area):
    ''' Load two dictionaries of GeoJSON geometries should be rendered.

        Dictionary keys are source paths, values are geometries
    '''
    good_geometries, bad_geometries = dict(), dict()

    osr.UseExceptions()
    sref_geo = osr.SpatialReference()
    sref_geo.ImportFromProj4(EPSG4326)
    sref_map = osr.SpatialReference()
    sref_map.ImportFromProj4(EPSG2163 if area == USA else ESRI54029)
    project = osr.CoordinateTransformation(sref_geo, sref_map)

    for path in iterate_sources_dir(directory):
        with open(join(directory, path)) as file:
            data = json.load(file)

        if 'geometry' in data.get('coverage', {}):
            geojson = json.dumps(data['coverage']['geometry'])
            geometry = ogr.CreateGeometryFromJson(geojson)

            if not geometry:
                continue

            geometry.Transform(project)

            if path in good_sources:
                good_geometries[path] = geometry
            else:
                bad_geometries[path] = geometry

    return good_geometries, bad_geometries
コード例 #4
0
 def Set(self, evt):
     try:
         from osgeo import osr
         osr.UseExceptions()
         source = osr.SpatialReference()
         # 0 = Custom
         if self.GetSelection() == 1:  # WGS84
             self.SetValue(config.epsg4326)
         elif self.GetSelection() == 2:  # UTM
             source.SetProjCS(
                 _("%s / UTM Zone %s%s") %
                 (config.wellknowngeogcs[self.geogcs.GetSelection()],
                  self.utmzone.GetValue(), ['N', 'S'
                                            ][self.north.GetSelection()]))
             source.SetWellKnownGeogCS(
                 config.wellknowngeogcs[self.geogcs.GetSelection()])
             source.SetUTM(self.utmzone.GetValue(),
                           (self.north.GetSelection() == 0))
             self.SetValue(source.ExportToPrettyWkt())
         elif self.GetSelection() == 3:  # EPSG
             if self.epsgesri.GetSelection() == 0:
                 source.ImportFromEPSG(self.epsgcode.GetValue())
             else:
                 source.ImportFromESRI(self.epsgcode.GetValue())
             self.SetValue(source.ExportToPrettyWkt())
     except Exception, error:
         wx.MessageBox("%s" % error, _("The SRS definition is not correct"),
                       wx.ICON_ERROR)
コード例 #5
0
def get_projection():
    '''
    '''
    osr.UseExceptions()
    sref_geo = osr.SpatialReference(); sref_geo.ImportFromProj4(EPSG4326)
    sref_map = osr.SpatialReference(); sref_map.ImportFromProj4(EPSG900913)
    return osr.CoordinateTransformation(sref_geo, sref_map)
コード例 #6
0
ファイル: _env.py プロジェクト: dgreyling/buzzard
def _set_up_osgeo_use_exception(new, _):
    if new:
        gdal.UseExceptions()
        osr.UseExceptions()
        ogr.UseExceptions()
    else:
        gdal.DontUseExceptions()
        osr.DontUseExceptions()
        ogr.DontUseExceptions()
コード例 #7
0
    def push_gdal_error_handler(cls):
        """ Install GDAL error handler """

        gdal.PushErrorHandler(cls.gdal_error_handler)

        gdal.UseExceptions()
        ogr.UseExceptions()
        osr.UseExceptions()

        cls.error_loaded = True
コード例 #8
0
def has_proj():
    sr1 = osr.SpatialReference()
    sr1.ImportFromEPSG(4326)  # lat, lon.
    sr2 = osr.SpatialReference()
    sr2.ImportFromEPSG(28355)  # GDA94/MGA zone 55.
    osrex = osr.GetUseExceptions()
    osr.UseExceptions()
    hasproj = True
    # Use exceptions to determine if we have proj and epsg files
    # otherwise we can't reliably determine if it has failed.
    try:
        trans = osr.CoordinateTransformation(sr1, sr2)
    except RuntimeError:
        hasproj = False
    return hasproj
コード例 #9
0
ファイル: dataset_record.py プロジェクト: alex-ip/agdc
 def define_transformation(self, dataset_crs, tile_crs):
     """Return the transformation between dataset_crs
     and tile_crs projections"""
     osr.UseExceptions()
     try:
         dataset_spatial_reference = self.create_spatial_ref(dataset_crs)
         tile_spatial_reference = self.create_spatial_ref(tile_crs)
         if dataset_spatial_reference is None:
             raise DatasetError('Unknown projecton %s' % str(dataset_crs))
         if tile_spatial_reference is None:
             raise DatasetError('Unknown projecton %s' % str(tile_crs))
         return osr.CoordinateTransformation(dataset_spatial_reference,
                                             tile_spatial_reference)
     except Exception:
         raise DatasetError('Coordinate transformation error ' +
                            'for transforming %s to %s' %
                            (str(dataset_crs), str(tile_crs)))
コード例 #10
0
ファイル: dataset_record.py プロジェクト: alex-ip/agdc
    def create_spatial_ref(crs):
        """Create a spatial reference system for projecton crs.
        Called by define_transformation()"""
        # pylint: disable=broad-except

        osr.UseExceptions()
        try:
            spatial_ref = osr.SpatialReference()
        except Exception:
            raise DatasetError('No spatial reference done for %s' % str(crs))
        try:
            spatial_ref.ImportFromWkt(crs)
            return spatial_ref
        except Exception:
            pass
        try:
            matchobj = re.match(r'EPSG:(\d+)', crs)
            epsg_code = int(matchobj.group(1))
            spatial_ref.ImportFromEPSG(epsg_code)
            return spatial_ref
        except Exception:
            return None
コード例 #11
0
    def svg(self, scale_factor=1.0, color=None):
        if self.is_empty:
            return '<g />'
        return '<g>%s</g>' % ''.join(
            p.svg(scale_factor, c) for p, c in zip(self, self.colors))


def load_utm_proj4(source_path):
    return GeoTable.load_utm_proj4(source_path)


def load(source_path, source_proj4=None, target_proj4=None, with_z=True, **kw):
    t = GeoTable.load(source_path, source_proj4, target_proj4, **kw)
    if not with_z:
        t['geometry_object'] = transform_geometries(t['geometry_object'],
                                                    drop_z)
    return t


concatenate_tables = partial(pd.concat, ignore_index=True)

gdal.SetConfigOption('GDAL_NUM_THREADS', 'ALL_CPUS')
gdal.UseExceptions()
ogr.UseExceptions()
osr.UseExceptions()
__all__ = [
    'GeoTable', 'GeoRow', 'GeoTableError', 'EmptyGeoTableError',
    'LONGITUDE_LATITUDE_PROJ4'
]
コード例 #12
0
ファイル: NED100m.py プロジェクト: Python3pkg/DEM-Tools
from math import floor, ceil, log
from os import unlink, close, write, makedirs, chmod
from os.path import basename, exists, isdir, join
from http.client import HTTPConnection
from urllib.parse import urlparse
from io import StringIO
from gzip import GzipFile
from hashlib import md5

from TileStache.Geography import SphericalMercator

from osgeo import gdal, osr

ideal_zoom = 11 ### log(3 * 360*360 / 256) / log(2) # ~10.6

osr.UseExceptions() # <-- otherwise errors will be silent and useless.

sref = osr.SpatialReference()
sref.ImportFromProj4('+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs')

def quads(minlon, minlat, maxlon, maxlat):
    """ Generate a list of northwest (lon, lat) for 1-degree quads of NED 10m data.
    """
    lon = floor(minlon)
    while lon <= maxlon:
    
        lat = ceil(maxlat)
        while lat >= minlat:
        
            yield lon, lat
        
コード例 #13
0
def test_gdal_calculations_py_2():
    ''' Test environment getting/setting '''
    try:
        from gdal_calculations import Env, Dataset
        from osgeo import gdal

        #Get/set cellsize
        assert Env.cellsize == "DEFAULT", "Env.cellsize != 'DEFAULT' ('%s')" % Env.cellsize
        Env.cellsize = "DEFAULT"
        Env.cellsize = "MINOF"
        Env.cellsize = "MAXOF"
        Env.cellsize = 1
        Env.cellsize = [1, 1]
        try:
            Env.cellsize = "INCORRECT"
        except:
            pass
        else:
            return fail('Env.cellsize accepted an incorrect value')

        #Get/set extent
        assert Env.extent == "MINOF", "Env.extent != 'MINOF' ('%s')" % Env.extent
        Env.extent = "MINOF"
        Env.extent = "MAXOF"
        Env.extent = [1, 1, 2, 2]
        try:
            Env.extent = "INCORRECT"
        except:
            pass
        else:
            return fail('Env.extent accepted an incorrect value')

        #Get/set extent
        assert Env.nodata == False, "Env.nodata != False ('%s')" % Env.nodata
        Env.nodata = True
        Env.nodata = False

        #Get/set overwrite
        assert Env.overwrite == False, "Env.overwrite != False ('%s')" % Env.overwrite
        Env.overwrite = True
        Env.overwrite = False

        #Get/set reproject
        assert Env.reproject == False, "Env.reproject != False ('%s')" % Env.reproject
        Env.reproject = True
        Env.reproject = False

        #Get/set resampling
        assert Env.resampling == gdal.GRA_NearestNeighbour, 'Env.resampling != "NEAREST"'
        Env.resampling = "BILINEAR"
        Env.resampling = "CUBIC"
        Env.resampling = "CUBICSPLINE"
        Env.resampling = "LANCZOS"
        Env.resampling = "NEAREST"
        if int(gdal.VersionInfo("VERSION_NUM")) >= 1100000:
            Env.resampling = "AVERAGE"
            Env.resampling = "MODE"
        try:
            Env.resampling = "INCORRECT"
        except:
            pass
        else:
            return fail('Env.resampling accepted an incorrect value')

        #Get/set snap
        assert Env.snap is None, 'Env.snap should be None not %s' % repr(
            Env.snap)
        Env.snap = Dataset('data/tgc_geo.tif')
        assert isinstance(Env.snap, Dataset), 'Env.snap is not a Dataset'

        #Get/set srs
        epsg = 4283
        wkt = 'GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]]'
        srs = osr.SpatialReference(wkt)
        assert Env.srs is None, 'Env.srs should be None'
        Env.srs = epsg
        assert isinstance(
            Env.srs,
            osr.SpatialReference), 'Env.srs is not a SpatialReference (EPSG)'
        Env.srs = wkt
        assert isinstance(
            Env.srs,
            osr.SpatialReference), 'Env.srs is not a SpatialReference (WKT)'
        Env.srs = srs
        assert isinstance(
            Env.srs, osr.SpatialReference
        ), 'Env.srs is not a SpatialReference (SpatialReference)'
        gue = osr.GetUseExceptions()
        osr.DontUseExceptions()
        try:
            Env.srs = 1
        except:
            pass
        else:
            return fail('Env.srs accepted an invalid EPSG (1)')
        try:
            Env.srs = 'INCORRECT'
        except:
            pass
        else:
            return fail('Env.extent accepted an invalid WKT ("INCORRECT")')
        try:
            Env.srs = osr.SpatialReference('sdfsdfsdf')
        except:
            pass
        else:
            return fail('Env.extent accepted an invalid SpatialReference')
        if gue: osr.UseExceptions()
        else: osr.DontUseExceptions()

        #Get/set tempdir
        try:
            tempdir = tempfile.tempdir  #use to reset tempfile.tempdir to default
            assert Env.tempdir == tempfile.tempdir, 'Env.tempdir != %s' % tempfile.tempdir
            Env.tempdir = 'tmp'  #autotest/pyscripts/tmp
            assert tempfile.tempdir == 'tmp', 'tempfile.tempdir != tmp'
            try:
                Env.tempdir = "INCORRECT"
            except:
                pass
            else:
                gdaltest.post_reason('Env.tempdir accepted an incorrect value')
                return 'fail'
        finally:
            tempfile.tempdir = tempdir

        #Get/set tiled
        assert Env.tiled == True, "Env.tiled != True ('%s')" % Env.tiled
        Env.tiled = False
        Env.tiled = True

        return 'success'
    except ImportError:
        return 'skip'
    except AssertionError:
        return fail()
    finally:
        cleanup()
コード例 #14
0
def SRSInput(srs):
	osr.UseExceptions()
	reference.SetFromUserInput(srs)
	return reference.ExportToPrettyWkt()
コード例 #15
0
ファイル: gdal.py プロジェクト: spatialhast/gdal2mbtiles
from itertools import count
import os
import re
from subprocess import CalledProcessError, check_output, Popen, PIPE
from tempfile import NamedTemporaryFile
from xml.etree import ElementTree

import numpy

from osgeo import gdal, gdalconst, osr
from osgeo.gdalconst import (GA_ReadOnly, GRA_Bilinear, GRA_Cubic,
                             GRA_CubicSpline, GRA_Lanczos,
                             GRA_NearestNeighbour)

gdal.UseExceptions()  # Make GDAL throw exceptions on error
osr.UseExceptions()  # And OSR as well.

from .constants import (EPSG_WEB_MERCATOR, ESRI_102113_PROJ, ESRI_102100_PROJ,
                        GDALTRANSLATE, GDALWARP, TILE_SIDE)
from .exceptions import (GdalError, CalledGdalError, UnalignedInputError,
                         UnknownResamplingMethodError)
from .types import Extents, GdalFormat, XY
from .utils import rmfile

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

RESAMPLING_METHODS = {
    GRA_NearestNeighbour: 'near',
    GRA_Bilinear: 'bilinear',
    GRA_Cubic: 'cubic',
コード例 #16
0
    def combine_bands(self):
        """
        Start combining the bands.
        A file is created in the process.
        """

        self.LOGGER.debug("merging bands to one file")

        osr.UseExceptions()

        band_list = []
        crs = self.layer_list[0][0].crs()
        cell_resolution_x = self.layer_list[0][0].rasterUnitsPerPixelX()
        cell_resolution_y = self.layer_list[0][0].rasterUnitsPerPixelY()
        x_min = self.layer_list[0][0].dataProvider().extent().xMinimum()
        x_max = self.layer_list[0][0].dataProvider().extent().xMaximum()
        y_min = self.layer_list[0][0].dataProvider().extent().yMinimum()
        y_max = self.layer_list[0][0].dataProvider().extent().yMaximum()
        for layer in self.layer_list:
            if crs != layer[0].crs():
                self.finished.emit(False, "Location does not match", None)
                self.LOGGER.warning("location does not match")
                return

            # location comparison
            if x_min != layer[0].dataProvider().extent().xMinimum() or \
                    x_max != layer[0].dataProvider().extent().xMaximum() or \
                    y_min != layer[0].dataProvider().extent().yMinimum() or \
                    y_max != layer[0].dataProvider().extent().yMaximum():
                self.LOGGER.warning("size does not match")
                self.finished.emit(False, "Image sizes does not match", None)
                return

            # finding the highest resolution
            if cell_resolution_x > layer[0].rasterUnitsPerPixelX():
                cell_resolution_x = layer[0].rasterUnitsPerPixelX()
            if cell_resolution_y > layer[0].rasterUnitsPerPixelY():
                cell_resolution_y = layer[0].rasterUnitsPerPixelY()

            band_list.append(self._layer_to_array(layer[0], layer[1]))

        wkt = crs.toWkt()
        band_list = self._equalize_arrays(band_list)

        height, width = band_list[0].shape

        self.LOGGER.debug("getting driver by name: self.gdal_driver_name")
        driver = gdal.GetDriverByName(self.gdal_driver_name)

        self.LOGGER.debug("creating output_raster")
        output_raster = driver.Create(self.output_path,
                                      width,
                                      height,
                                      len(band_list),
                                      eType=self.gdal_data_type)

        geo_transform = (x_min, cell_resolution_x, 0, y_max, 0,
                         -1 * cell_resolution_y)

        output_raster.SetProjection(str(wkt))
        output_raster.SetGeoTransform(geo_transform)

        for index in range(0, len(band_list)):
            output_band = output_raster.GetRasterBand(index + 1)
            output_band.WriteArray(band_list[index])
            output_band.FlushCache()

        if not os.path.exists(self.output_path):
            self.LOGGER.warning("result file was not created",
                                self.output_path)
            self.finished.emit(False, "Result file was not created", None)

        self.finished.emit(True, "Success", self.output_path)