OGR_t1
Commands
- ogrinfo: Lists information about an OGR-supported data source.
- ogr2ogr: Converts simple features data between file formats.
- ogrtindex: Creates a tileindex.
- ogrlineref: Create linear reference and provide some calculations using it.
- ogrmerge.py: Merge several vector datasets into a single one.
- ogr_layer_algebra.py: Performs various Vector layer algebraic operations.
In [5]:
cd /Users/abharathi/Documents/gis_data/gdal-tools
In [6]:
ls
In [7]:
!ogrinfo worldcities.csv
In [9]:
!ogrinfo -al -so worldcities.csv
In [10]:
%%time
!ogr2ogr -f GPKG worldcities.gpkg worldcities.csv \
-oo X_POSSIBLE_NAMES=lng -oo Y_POSSIBLE_NAMES=lat -a_srs EPSG:4326
In [11]:
%%time
!ogr2ogr -f GPKG worldcities.gpkg worldcities.csv \
-oo X_POSSIBLE_NAMES=lng -oo Y_POSSIBLE_NAMES=lat -a_srs EPSG:4326 \
-sql "SELECT city, country, CAST(population AS integer) as population from worldcities where country = 'India'"
In [12]:
%%time
!ogr2ogr -f GPKG worldcities.gpkg worldcities.csv \
-oo X_POSSIBLE_NAMES=lng -oo Y_POSSIBLE_NAMES=lat -a_srs EPSG:4326 \
-sql "SELECT city, country, CAST(population AS integer) as population from worldcities where country = 'India'" \
-nln mycities
In [14]:
!ogrinfo -so -al worldcities.gpkg
In [16]:
!ogr2ogr mycities.shp worldcities.gpkg \
-t_srs EPSG:7755 -lco ENCODING=UTF-8 \
-lco SPATIAL_INDEX=Yes
In [18]:
ls
In [17]:
# Get country pop from city pop
!ogr2ogr country_pop.csv worldcities.gpkg \
-sql "SELECT country, sum(population) as total_population from worldcities GROUP BY country"
In [19]:
cd earthquakes/
In [20]:
ls
In [25]:
!which ogrmerge.py
In [26]:
%run -i /Users/abharathi/micromamba/envs/opengeo/bin/ogrmerge.py --help
In [28]:
%run -i /Users/abharathi/micromamba/envs/opengeo/bin/ogrmerge.py -o earthquakes.gpkg *.geojson \
-single -nln all_earthquakes -overwrite_ds
In [32]:
!ogr2ogr earthquakes.gpkg earthquakes.gpkg \
-where "mag>4.5" -nln large_earthquakes -update
In [33]:
cd ..
In [37]:
# %load batch.py
import os
input_dir = 'naip'
command = 'gdal_translate -of GTiff -co COMPRESS=JPEG {input} {output}'
for file in os.listdir(input_dir):
if file.endswith('.jp2'):
input = os.path.join(input_dir, file)
filename = os.path.splitext(os.path.basename(file))[0]
output = os.path.join(input_dir, filename + '.tif')
os.system(command.format(input=input, output=output))
In [ ]:
# %load batch_parallel.py
import os
from multiprocessing import Pool
from timeit import default_timer as timer
input_dir = 'naip'
command = 'gdal_translate -of GTiff -co COMPRESS=JPEG {input} {output}'
def process(file):
input = os.path.join(input_dir, file)
filename = os.path.splitext(os.path.basename(file))[0]
output = os.path.join(input_dir, filename + '.tif')
os.system(command.format(input=input, output=output))
files = [file for file in os.listdir(input_dir) if file.endswith('.jp2')]
if __name__ == '__main__':
start = timer()
p = Pool(4)
p.map(process, files)
end = timer()
print(end - start)
# start = timer()
# for file in files:
# process(file)
# end = timer()
# print(end - start)
In [ ]: