Beispiel #1
0
def polygonize_scene(f_img, f_vct):
    #-------------- generate original shaplefile, complex polygon and in a UTM projection
    f_temp = os.path.split(f_vct)[0] + '/lakes_ori.shp'
    ds_img = gdal.Open(f_img)
    band = ds_img.GetRasterBand(1)

    projection = ds_img.GetProjection()
    geo_transform = ds_img.GetGeoTransform()

    driver = ogr.GetDriverByName("ESRI Shapefile")
    if os.path.exists(f_temp):
        driver.DeleteDataSource(f_temp)

    ds_out = driver.CreateDataSource(f_temp)
    layer_out = ds_out.CreateLayer("polygonized", srs=None)
    field_new = ogr.FieldDefn('Code_uniq', ogr.OFTInteger)
    layer_out.CreateField(field_new)

    gdal.Polygonize(band, None, layer_out, 0, [], callback=None)

    ds_out.Destroy()
    del ds_img

    f_prj = f_temp[:-3] + 'prj'
    fo_prj = open(f_prj, 'w')
    fo_prj.write(projection)
    fo_prj.close()
    #-------------- simplify and reprojct
    cmd_ogr = 'ogr2ogr -overwrite -simplify 30 -where \"Code_uniq>0\"  -t_srs "+proj=aea +lat_1=25.0 +lat_2=47.0 +lon_0=105.0 +x_0=0 +y_0=0 +datum=WGS84" ' + f_vct + ' ' + f_temp

    _rs = lib_IO.run_exe(cmd_ogr)
    print f_vct

    driver = ogr.GetDriverByName("ESRI Shapefile")
    driver.DeleteDataSource(f_temp)
Beispiel #2
0
def polygonize_scene(p_render,f_img,f_vct):
	#-------------- generate original shaplefile, complex polygon and in a UTM projection
	f_temp = p_render + '/lakes_ori.shp'
	ds_img = gdal.Open(f_img)
	band = ds_img.GetRasterBand(1)
	
	projection = ds_img.GetProjection()
	geo_transform = ds_img.GetGeoTransform()

	driver = ogr.GetDriverByName("ESRI Shapefile")
	if os.path.exists(f_temp):
		driver.DeleteDataSource(f_temp)
		
	sr_in = osr.SpatialReference()
	sr_in.ImportFromWkt(projection)
	ds_out = driver.CreateDataSource(f_temp)
	layer_out = ds_out.CreateLayer("polygonized", srs=sr_in)
	sub__add_fields(layer_out)
	
	gdal.Polygonize( band, None, layer_out, 0, [], callback=None )
	
	sub__update_field_value(f_vct,layer_out)
	
	ds_out.Destroy()
	del ds_img
	
	#-------------- simplify and reprojct
	cmd_ogr = 'ogr2ogr -overwrite -simplify 25 -where \"Code_uniq>0\" ' + f_vct + ' ' + f_temp

	_rs = lib_IO.run_exe(cmd_ogr)
	print f_vct
	
	driver = ogr.GetDriverByName("ESRI Shapefile")
Beispiel #3
0
def upscaling_raster(fs_use,resolution):
	fs_out = []
	for f_in in fs_use:
		f_out = f_in[:-7] + str(resolution)+ 'm.tif'
	
		cmd_gdal = 'gdalwarp -tr ' + str(resolution) + ' ' + str(resolution) + ' -r average -overwrite -multi -srcnodata -9999 -dstnodata -9999 ' + f_in + ' ' + f_out
		if not os.path.isfile(f_out):
			_rs = lib_IO.run_exe(cmd_gdal)
		print f_out
		fs_out.append(f_out)

	
	f_pct = os.path.split(f_in)[0] + '/pct.tif'
	str_files = ' '.join(fs_out)
	cmd_merge = 'gdal_merge.py -separate -pct -o ' + f_pct + ' ' + str_files
	_rs = lib_IO.run_exe(cmd_merge)
	print f_pct
	return f_pct,fs_out
Beispiel #4
0
def upscaling_raster(fs_use, resolution):
    fs_out = []
    for f_in in fs_use:
        f_out = f_in[:-7] + str(resolution) + 'm.tif'

        cmd_gdal = 'gdalwarp -tr ' + str(resolution) + ' ' + str(
            resolution
        ) + ' -r average -overwrite -multi -srcnodata -9999 -dstnodata -9999 ' + f_in + ' ' + f_out
        if not os.path.isfile(f_out):
            _rs = lib_IO.run_exe(cmd_gdal)
        print f_out
        fs_out.append(f_out)

    f_pct = os.path.split(f_in)[0] + '/pct.tif'
    str_files = ' '.join(fs_out)
    cmd_merge = 'gdal_merge.py -separate -pct -o ' + f_pct + ' ' + str_files
    _rs = lib_IO.run_exe(cmd_merge)
    print f_pct
    return f_pct, fs_out
Beispiel #5
0
def merge_lakes_shp(dic_data_path):
	p_out = dic_data_path['path_out']
	p_vector = dic_data_path['path_vector']
	name_wi = dic_data_path['water_index']
	f_merged = p_vector + '/merged_' + name_wi[:-4] + '.shp'
	f_ref = p_vector + '/' + dic_data_path['base_shp'][:-4] + '_ui.shp'

	#'''
	ls_pr = lib_IO.getDirList( p_out ,'p...r...')

	fs_lake_shp = []
	for pr in ls_pr:
		p_pr = p_out + '/' + pr
		name_scene = lib_IO.getDirList(p_pr ,'L.*')[0]
		p_out_scene = p_pr + '/' + name_scene
		f_lake_shp = p_out_scene + '/lakes_' + name_wi[:-4] + '.shp'
		if os.path.isfile(f_lake_shp):
			fs_lake_shp.append(f_lake_shp)
		else:
			print 'failure on ',f_lake_shp
	
	cmd_ogr = 'ogr2ogr -overwrite ' + f_merged + ' ' + fs_lake_shp[0]
	_rs = lib_IO.run_exe(cmd_ogr)
	#print fs_lake_shp[0]
	
	for f in fs_lake_shp[1:]:
		cmd_ogr = 'ogr2ogr -update -append ' + f_merged + ' ' + f
		_rs = lib_IO.run_exe(cmd_ogr)
		#print f
	print f_merged
	#'''
	#f_pr = p_vector + 'qt2000_tile.shp'
	f_pr = p_vector + '/requires/image_extent.shp'

	dic_ui_wopr = lib_overlaps.analysis_choose_or_union(f_ref,f_pr)
	f_dislv = p_vector + '/dissolved_' + name_wi[:-4] + '.shp'
	dissolve_polygons_shp(f_merged,f_ref,dic_ui_wopr,f_dislv)
	print f_dislv
Beispiel #6
0
def upscaling_raster(fs_use,p_render,resolution):
	fs_out = []
	for f_in in fs_use:
		name_img = os.path.split(f_in)[-1]
		if f_in[-5] == 'm':
			f_out = p_render + '/' + name_img[:-7] + str(resolution)+ 'm.tif'
		else:
			f_out = p_render + '/' + name_img[:-4] + '_' + str(resolution)+ 'm.tif'
	
		cmd_gdal = 'gdalwarp -tr ' + str(resolution) + ' ' + str(resolution) + ' -r average -overwrite -multi -srcnodata -9999 -dstnodata -9999 ' + f_in + ' ' + f_out
		if not os.path.isfile(f_out):
			_rs = lib_IO.run_exe(cmd_gdal)
		print f_out
		fs_out.append(f_out)
	if len(fs_use) == 1:
		return f_out,fs_out
	
	f_pct = p_render + '/pct.tif'
	str_files = ' '.join(fs_out)
	cmd_merge = 'gdal_merge.py -separate -pct -o ' + f_pct + ' ' + str_files
	_rs = lib_IO.run_exe(cmd_merge)
	print f_pct
	return f_pct,fs_out
Beispiel #7
0
def get_data_extent(pathrow,p_scene):
	f_img = p_scene + '/mask.img'
	f_img_tmp = p_scene + '/data_ext_tmp.img'
	geo_transform,projection = gen_tmp_extent(f_img,f_img_tmp)

	f_vct_tmp = p_scene + '/data_extent_ori.shp'
	f_vct = p_scene + '/data_extent.shp'
	
	ds_img = gdal.Open(f_img_tmp)
	band = ds_img.GetRasterBand(1)
	
	driver = ogr.GetDriverByName("ESRI Shapefile")
	if os.path.exists(f_vct_tmp):
		driver.DeleteDataSource(f_vct_tmp)
		
	sr_in = osr.SpatialReference()
	sr_in.ImportFromWkt(projection)
	ds_out = driver.CreateDataSource(f_vct_tmp)
	layer_out = ds_out.CreateLayer("polygonized", srs=sr_in)
	sub__add_fields(layer_out)
	
	gdal.Polygonize( band, None, layer_out, 0, [], callback=None )
	
	sub__update_field_value(f_vct,layer_out,pathrow)
	
	ds_out.Destroy()
	del ds_img

	#-------------- simplify and reprojct
	cmd_ogr = 'ogr2ogr -overwrite -where \"PValue=1\"  -t_srs "+proj=aea +lat_1=25.0 +lat_2=47.0 +lon_0=105.0 +x_0=0 +y_0=0 +datum=WGS84" ' + f_vct + ' ' + f_vct_tmp

	_rs = lib_IO.run_exe(cmd_ogr)
	print f_vct
	
	driver = ogr.GetDriverByName("ESRI Shapefile")
	driver.DeleteDataSource(f_vct_tmp)
	os.remove(f_img_tmp)
Beispiel #8
0
def get_data_extent(pathrow, p_scene):
    f_img = p_scene + '/mask.img'
    f_img_tmp = p_scene + '/data_ext_tmp.img'
    geo_transform, projection = gen_tmp_extent(f_img, f_img_tmp)

    f_vct_tmp = p_scene + '/data_extent_ori.shp'
    f_vct = p_scene + '/data_extent.shp'

    ds_img = gdal.Open(f_img_tmp)
    band = ds_img.GetRasterBand(1)

    driver = ogr.GetDriverByName("ESRI Shapefile")
    if os.path.exists(f_vct_tmp):
        driver.DeleteDataSource(f_vct_tmp)

    sr_in = osr.SpatialReference()
    sr_in.ImportFromWkt(projection)
    ds_out = driver.CreateDataSource(f_vct_tmp)
    layer_out = ds_out.CreateLayer("polygonized", srs=sr_in)
    sub__add_fields(layer_out)

    gdal.Polygonize(band, None, layer_out, 0, [], callback=None)

    sub__update_field_value(f_vct, layer_out, pathrow)

    ds_out.Destroy()
    del ds_img

    #-------------- simplify and reprojct
    cmd_ogr = 'ogr2ogr -overwrite -where \"PValue=1\"  -t_srs "+proj=aea +lat_1=25.0 +lat_2=47.0 +lon_0=105.0 +x_0=0 +y_0=0 +datum=WGS84" ' + f_vct + ' ' + f_vct_tmp

    _rs = lib_IO.run_exe(cmd_ogr)
    print f_vct

    driver = ogr.GetDriverByName("ESRI Shapefile")
    driver.DeleteDataSource(f_vct_tmp)
    os.remove(f_img_tmp)
Beispiel #9
0
def dissolve_polygons_cmd(f_merged, f_final):
    cmd_ogr = 'ogr2ogr -f "ESRI Shapefile" ' + f_final + ' ' + f_merged + ' -dialect sqlite -sql "select ST_union(Geometry),Code_uniq from input GROUP BY Code_uniq"'
    _rs = lib_IO.run_exe(cmd_ogr)
    print f_final