def parse_font_spec(self, font_spec): """ parse a font specification """ self.gdk_font = None result = '' #coerce XLFDFontSpec classes into strings to parse them font_spec = str(font_spec) if font_spec[0:1] != '-': gdal.Debug("pgufont", "invalid XLFD(%s), should start with -" % font_spec) return new_parts = string.split(font_spec, '-') del new_parts[0] #remove first (empty) part produced by split if len(new_parts) != 14: gdal.Debug("pgufont", 'invalid XLFD(%s), should have 14 parts' % font_spec) return else: self.parts = new_parts
def BandWriteArray(band, array, xoff=0, yoff=0, resample_alg=gdal.GRIORA_NearestNeighbour, callback=None, callback_data=None): """Pure python implementation of writing a chunk of a GDAL file from a numpy array. Used by the gdal.Band.WriteArray method.""" if array is None or len(array.shape) != 2: raise ValueError("expected array of dim 2") xsize = array.shape[1] ysize = array.shape[0] if xsize + xoff > band.XSize or ysize + yoff > band.YSize: raise ValueError("array larger than output file, or offset off edge") datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) # if we receive some odd type, like int64, try casting to a very # generic type we do support (#2285) if not datatype: gdal.Debug('gdal_array', 'force array to float64') array = array.astype(numpy.float64) datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) if not datatype: raise ValueError("array does not have corresponding GDAL data type") return BandRasterIONumPy(band, 1, xoff, yoff, xsize, ysize, array, datatype, resample_alg, callback, callback_data)
def MDArrayWriteArray(mdarray, array, array_start_idx = None, array_step = None): if not array_start_idx: array_start_idx = [0] * mdarray.GetDimensionCount() if not array_step: array_step = [1] * mdarray.GetDimensionCount() buffer_datatype = mdarray.GetDataType() if array.dtype != ExtendedDataTypeToNumPyDataType(buffer_datatype): datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) # if we receive some odd type, like int64, try casting to a very # generic type we do support (#2285) if not datatype: gdal.Debug('gdal_array', 'force array to float64') array = array.astype(numpy.float64) datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) if not datatype: raise ValueError("array does not have corresponding GDAL data type") buffer_datatype = gdal.ExtendedDataType.Create(datatype) ret = MDArrayIONumPy(True, mdarray, array, array_start_idx, array_step, buffer_datatype) if ret != 0: _RaiseException() return ret
def BandWriteArray( band, array, xoff=0, yoff=0 ): """Pure python implementation of writing a chunk of a GDAL file from a numpy array. Used by the gdal.Band.WriteAsArray method.""" xsize = array.shape[1] ysize = array.shape[0] if xsize + xoff > band.XSize or ysize + yoff > band.YSize: raise ValueError("array larger than output file, or offset off edge") datatype = NumericTypeCodeToGDALTypeCode( array.dtype.type ) # if we receive some odd type, like int64, try casting to a very # generic type we do support (#2285) if not datatype: gdal.Debug( 'gdal_array', 'force array to float64' ) array = array.astype( numpy.float64 ) datatype = NumericTypeCodeToGDALTypeCode( array.dtype.type ) if not datatype: raise ValueError("array does not have corresponding GDAL data type") result = band.WriteRaster( xoff, yoff, xsize, ysize, array.tostring(), xsize, ysize, datatype ) return result
def set_font_part(self, field_name, value): """ set a part of the font description """ self.gdk_font = None if self.xlfd_field_names.count(field_name) != 0: self.parts[self.xlfd_field_names.index(field_name)] = value else: gdal.Debug( "pgufont", 'invalid field name (%s), should be one of %s' \ % (field_name, self.xlfd_field_names) )
def set_locale(locale_name): """ change locales to the specified locale name locale_name - string, the name of the locale (must be one of the keys in 'locales' """ global _locales try: fname = _locales[locale_name] _load_locale(fname) except: gdal.Debug("nls", 'locale %s not found' % locale_name)
def _load_locale(fname): """ open a locale file and set the internal locale fname - string, the name of the locale file to load """ global _locale try: file = open(fname, 'r') except: gdal.Debug("nls", 'load locale failed') _locale = None return loc_dict = {} lines = file.readlines() for line in lines: if line[0] == '#': continue try: if string.find(line, '=') >= 0: key, value = string.split(line, '=') #take care with CR-LF #does this work on UNIX? value = string.rstrip(value) value = string.replace(value, '\\n', '\n') loc_dict[key] = value except: gdal.Debug("nls", 'an exception occurred in nls.py') pass file.close() _locale = loc_dict try: gdal.Debug("nls", 'locale changed to %s' % loc_dict['locale-name']) except: gdal.Debug("nls", 'unknown locale loaded')
def get_font_part(self, field_name=None, field_number=None): """ Get one part of the font description by field name or number. If both are specified, the result of the field name will be returned if it is valid, or if invalid, the field number, or if that is also invalid, the an empty string. """ result = '' if field_number is not None: if field_number < 1 or field_number > 14: gdal.Debug( "pgufont", 'invalid field number (%s), should be 1-14' % field_number) else: result = self.parts[field_number] if field_name is not None: if self.xlfd_field_names.count(field_name) == 0: gdal.Debug( "pgufont", 'invalid field name (%s), should be one of %s' \ % (field_name, self.xlfd_field_names) ) else: result = self.parts[self.xlfd_field_names.index(field_name)] return result
def __init__(self, filterspec): """ Initialize the filter based on the spec string """ #parse the filterspec parts = filterspec.split("|" ) if len(parts) != 2: gdal.Debug( "OpenEV", "an error occurred parsing the FilterSpec %s" % filterspec ) pass self.name = parts[0].strip() filters = parts[1].split( "," ) self.filters = [] for i in range(len(filters)): re_filter = string.replace(string.strip(filters[i]), "\\", "\\\\") re_filter = string.replace(re_filter, ".", "\.") re_filter = string.replace(re_filter, "*", ".*?") re_comp = re.compile(re_filter, re.I) self.filters.append( re_comp )
def set_filter(self, filter): """programmatically set the filename filter""" self.filters = {} self.filter_keys = [] self.filter = None #build the filters if filter != None and len(filter) > 0: filterSpecs = filter.split( "|" ) if len(filterSpecs) % 2 != 0: gdal.Debug( "OpenEV", "invalid filter spec: %s" % filter ) pass else: for i in range(0, len(filterSpecs), 2): spec = FilterSpec(filterSpecs[i] + "|" + filterSpecs[i+1]) self.filters[spec.name] = spec self.filter_keys.append( spec.name ) else: self.filters[nls.get("filedlg-default-filter", "All Files (*.*)|*.*")] = FilterSpec( nls.get("filedlg-default-filter", "All Files (*.*)|*.*") ) self.cmb_filter.set_popdown_strings( self.filter_keys ) self.filter = self.filters[self.cmb_filter.entry.get_text()]
get(key, default) - gets the localized value of key or default if not found """ _locales = {} _locale = None #load locale files from the locale directory and parse them for locale #names ... this only happens on the first import in any particular #python run-time import gview import os import string import gdal gdal.Debug("nls", 'initializing localization module') locale_dir = os.path.join(gview.find_gview(), "locales") if os.path.isdir(locale_dir): gdal.Debug("nls", 'loading localization files from %s' % locale_dir) fnames = os.listdir(locale_dir) for fname in fnames: fname = os.path.join(locale_dir, fname) gdal.Debug("nls", 'processing file %s' % fname) if os.path.isfile(fname): file = open(fname, 'r') loc_dict = {} lines = file.readlines() for line in lines: if string.find(line, '=') >= 0:
def DatasetWriteArray(ds, array, xoff=0, yoff=0, band_list=None, interleave='band', resample_alg=gdal.GRIORA_NearestNeighbour, callback=None, callback_data=None): """Pure python implementation of writing a chunk of a GDAL file from a numpy array. Used by the gdal.Dataset.WriteArray method.""" if band_list is None: band_list = list(range(1, ds.RasterCount + 1)) interleave = interleave.lower() if interleave == 'band': interleave = True xdim = 2 ydim = 1 banddim = 0 elif interleave == 'pixel': interleave = False xdim = 1 ydim = 0 banddim = 2 else: raise ValueError('Interleave should be band or pixel') if len(band_list) == 1: if array is None or (len(array.shape) != 2 and len(array.shape) != 3): raise ValueError("expected array of dim 2 or 3") if len(array.shape) == 3: if array.shape[banddim] != 1: raise ValueError("expected size of dimension %d should be 1" % banddim) array = array[banddim] return BandWriteArray(ds.GetRasterBand(band_list[0]), array, xoff=xoff, yoff=yoff, resample_alg=resample_alg, callback=callback, callback_data=callback_data) if array is None or len(array.shape) != 3: raise ValueError("expected array of dim 3") xsize = array.shape[xdim] ysize = array.shape[ydim] if xsize + xoff > ds.RasterXSize or ysize + yoff > ds.RasterYSize: raise ValueError("array larger than output file, or offset off edge") if array.shape[banddim] != len(band_list): raise ValueError('Dimension %d of array should have size %d to store bands)' % (banddim, len(band_list))) datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) # if we receive some odd type, like int64, try casting to a very # generic type we do support (#2285) if not datatype: gdal.Debug('gdal_array', 'force array to float64') array = array.astype(numpy.float64) datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type) if not datatype: raise ValueError("array does not have corresponding GDAL data type") ret = DatasetIONumPy(ds, 1, xoff, yoff, xsize, ysize, array, datatype, resample_alg, callback, callback_data, interleave, band_list) if ret != 0: _RaiseException() return ret