From 80e189fb37a1625003e29b6224890e0a45ef7a0c Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Thu, 4 Apr 2024 20:50:03 -0400 Subject: [PATCH] Python: remove ogr2ogr.py sample --- autotest/pyscripts/test_ogr2ogr_py.py | 1695 --------------- doc/source/api/python_samples.rst | 1 - .../gdal-utils/osgeo_utils/samples/ogr2ogr.py | 1920 ----------------- 3 files changed, 3616 deletions(-) delete mode 100755 autotest/pyscripts/test_ogr2ogr_py.py delete mode 100644 swig/python/gdal-utils/osgeo_utils/samples/ogr2ogr.py diff --git a/autotest/pyscripts/test_ogr2ogr_py.py b/autotest/pyscripts/test_ogr2ogr_py.py deleted file mode 100755 index e99bd7688dfb..000000000000 --- a/autotest/pyscripts/test_ogr2ogr_py.py +++ /dev/null @@ -1,1695 +0,0 @@ -#!/usr/bin/env pytest -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: ogr2ogr.py testing -# Author: Even Rouault -# -############################################################################### -# Copyright (c) 2010-2013, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - -import os -import shutil -import sys - -import pytest - -sys.path.append("../ogr") - -import gdaltest -import ogrtest -import test_cli_utilities -import test_py_scripts - -from osgeo import gdal, ogr, osr - -pytestmark = pytest.mark.skipif( - test_py_scripts.get_py_script("ogr2ogr") is None, - reason="ogr2ogr.py not available", -) - - -@pytest.fixture() -def script_path(): - return test_py_scripts.get_py_script("ogr2ogr") - - -############################################################################### -# Simple test - - -def test_ogr2ogr_py_1(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except Exception: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - - feat0 = ds.GetLayer(0).GetFeature(0) - assert ( - feat0.GetFieldAsDouble("AREA") == 215229.266 - ), "Did not get expected value for field AREA" - assert ( - feat0.GetFieldAsString("PRFEDEA") == "35043411" - ), "Did not get expected value for field PRFEDEA" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -sql - - -def test_ogr2ogr_py_2(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except Exception: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -sql "select * from poly"', - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -spat - - -def test_ogr2ogr_py_3(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except Exception: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -spat 479609 4764629 479764 4764817", - ) - - ds = ogr.Open("tmp/poly.shp") - if ogrtest.have_geos(): - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - else: - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 5 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -where - - -def test_ogr2ogr_py_4(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except (OSError, AttributeError): - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -where "EAS_ID=171"', - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 1 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -append - - -def test_ogr2ogr_py_5(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except (OSError, AttributeError): - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-update -append tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 20 - - feat10 = ds.GetLayer(0).GetFeature(10) - assert ( - feat10.GetFieldAsDouble("AREA") == 215229.266 - ), "Did not get expected value for field AREA" - assert ( - feat10.GetFieldAsString("PRFEDEA") == "35043411" - ), "Did not get expected value for field PRFEDEA" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -overwrite - - -def test_ogr2ogr_py_6(script_path, pg_ds): - - if test_cli_utilities.get_ogrinfo_path() is None: - pytest.skip() - - assert pg_ds.GetLayerByName("tpoly") is None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-f PostgreSQL " - + f'"{pg_ds.GetDescription()}" ' - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln tpoly", - ) - - assert pg_ds.GetLayerByName("tpoly").GetFeatureCount() == 10 - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-update -overwrite -f PostgreSQL " - + f'"{pg_ds.GetDescription()}" ' - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln tpoly", - ) - - assert pg_ds.GetLayerByName("tpoly").GetFeatureCount() == 10 - - -############################################################################### -# Test -gt - - -def test_ogr2ogr_py_7(script_path, pg_ds): - - if test_cli_utilities.get_ogrinfo_path() is None: - pytest.skip() - - assert pg_ds.GetLayerByName("tpoly") is None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-f PostgreSQL " - + f'"{pg_ds.GetDescription()}" ' - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln tpoly -gt 1", - ) - - assert pg_ds.GetLayerByName("tpoly").GetFeatureCount() == 10 - - -############################################################################### -# Test -t_srs - - -def test_ogr2ogr_py_8(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-t_srs EPSG:4326 tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert str(ds.GetLayer(0).GetSpatialRef()).find("1984") != -1 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -a_srs - - -def test_ogr2ogr_py_9(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-a_srs EPSG:4326 tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert str(ds.GetLayer(0).GetSpatialRef()).find("1984") != -1 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -select - - -def test_ogr2ogr_py_10(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - # Voluntary don't use the exact case of the source field names (#4502) - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-select eas_id,prfedea tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - lyr = ds.GetLayer(0) - assert lyr.GetLayerDefn().GetFieldCount() == 2 - feat = lyr.GetNextFeature() - assert feat.GetFieldAsDouble("EAS_ID") == 168 - assert feat.GetFieldAsString("PRFEDEA") == "35043411" - feat = None - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -lco - - -def test_ogr2ogr_py_11(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-lco SHPT=POLYGONZ tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds.GetLayer(0).GetLayerDefn().GetGeomType() == ogr.wkbPolygon25D - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -nlt - - -def test_ogr2ogr_py_12(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-nlt POLYGON25D tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds.GetLayer(0).GetLayerDefn().GetGeomType() == ogr.wkbPolygon25D - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Add explicit source layer name - - -def test_ogr2ogr_py_13(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp poly", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -segmentize - - -@pytest.mark.skip(reason="-segmentize not implemented") -def test_ogr2ogr_py_14(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-segmentize 100 tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp poly", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - feat = ds.GetLayer(0).GetNextFeature() - assert feat.GetGeometryRef().GetGeometryRef(0).GetPointCount() == 36 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -overwrite with a shapefile - - -def test_ogr2ogr_py_15(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - # Overwrite - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-overwrite tmp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -fid - - -def test_ogr2ogr_py_16(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-fid 8 tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - - src_ds = ogr.Open(test_py_scripts.get_data_path("ogr") + "poly.shp") - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 1 - src_feat = src_ds.GetLayer(0).GetFeature(8) - feat = ds.GetLayer(0).GetNextFeature() - assert feat.GetField("EAS_ID") == src_feat.GetField("EAS_ID") - ds.Destroy() - src_ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -progress - - -def test_ogr2ogr_py_17(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - ret = test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-progress tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - assert ( - ret.find("0...10...20...30...40...50...60...70...80...90...100 - done.") != -1 - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -wrapdateline - - -@pytest.mark.skip(reason="-wrapdateline not implemented") -@pytest.mark.require_geos -def test_ogr2ogr_py_18(script_path): - - try: - os.stat("tmp/wrapdateline_src.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/wrapdateline_src.shp" - ) - except OSError: - pass - - try: - os.stat("tmp/wrapdateline_dst.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/wrapdateline_dst.shp" - ) - except OSError: - pass - - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource( - "tmp/wrapdateline_src.shp" - ) - srs = osr.SpatialReference() - srs.ImportFromEPSG(32660) - lyr = ds.CreateLayer("wrapdateline_src", srs=srs) - feat = ogr.Feature(lyr.GetLayerDefn()) - geom = ogr.CreateGeometryFromWkt( - "POLYGON((700000 4000000,800000 4000000,800000 3000000,700000 3000000,700000 4000000))" - ) - feat.SetGeometryDirectly(geom) - lyr.CreateFeature(feat) - feat.Destroy() - ds.Destroy() - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-wrapdateline -t_srs EPSG:4326 tmp/wrapdateline_dst.shp tmp/wrapdateline_src.shp", - ) - - expected_wkt = "MULTIPOLYGON (((179.222391385437419 36.124095832129363,180.0 36.10605558800065,180.0 27.090340569400169,179.017505655195095 27.107979523625211,179.222391385437419 36.124095832129363)),((-180.0 36.10605558800065,-179.667822828781084 36.098349195413753,-179.974688335419557 27.089886143076747,-180.0 27.090340569400169,-180.0 36.10605558800065)))" - expected_geom = ogr.CreateGeometryFromWkt(expected_wkt) - ds = ogr.Open("tmp/wrapdateline_dst.shp") - lyr = ds.GetLayer(0) - feat = lyr.GetNextFeature() - ret = ogrtest.check_feature_geometry(feat, expected_geom) - feat.Destroy() - expected_geom.Destroy() - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/wrapdateline_src.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/wrapdateline_dst.shp") - - assert ret == 0 - - -############################################################################### -# Test -clipsrc - - -@pytest.mark.require_geos -def test_ogr2ogr_py_19(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -clipsrc spat_extent -spat 479609 4764629 479764 4764817", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - - assert ds.GetLayer(0).GetExtent() == ( - 479609, - 479764, - 4764629, - 4764817, - ), "unexpected extent" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test correct remap of fields when laundering to Shapefile format -# Test that the data is going into the right field -# FIXME: Any field is skipped if a subsequent field with same name is found. - - -@pytest.mark.require_driver("CSV") -def test_ogr2ogr_py_20(script_path): - - try: - os.remove("tmp/Fields.dbf") - except OSError: - pass - - expected_fields = [ - "a", - "A_1", - "a_1_2", - "aaaaaAAAAA", - "aAaaaAAA_1", - "aaaaaAAAAB", - "aaaaaAAA_2", - "aaaaaAAA_3", - "aaaaaAAA_4", - "aaaaaAAA_5", - "aaaaaAAA_6", - "aaaaaAAA_7", - "aaaaaAAA_8", - "aaaaaAAA_9", - "aaaaaAAA10", - ] - expected_data = [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - "11", - "12", - "13", - "14", - "15", - ] - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp " + test_py_scripts.get_data_path("utilities") + "Fields.csv", - ) - - ds = ogr.Open("tmp/Fields.dbf") - - assert ds is not None - layer_defn = ds.GetLayer(0).GetLayerDefn() - if layer_defn.GetFieldCount() != 15: - ds.Destroy() - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/Fields.dbf") - pytest.fail( - "Unexpected field count: " - + str(ds.GetLayer(0).GetLayerDefn().GetFieldCount()) - ) - - error_occurred = False - feat = ds.GetLayer(0).GetNextFeature() - for i in range(layer_defn.GetFieldCount()): - if layer_defn.GetFieldDefn(i).GetNameRef() != expected_fields[i]: - print( - "Expected ", - expected_fields[i], - ",but got", - layer_defn.GetFieldDefn(i).GetNameRef(), - ) - error_occurred = True - if feat.GetFieldAsString(i) != expected_data[i]: - print( - "Expected the value ", - expected_data[i], - ",but got", - feat.GetFieldAsString(i), - ) - error_occurred = True - - ds.Destroy() - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/Fields.dbf") - - assert not error_occurred - - -############################################################################### -# Test ogr2ogr when the output driver has already created the fields -# at dataset creation (#3247) - - -@pytest.mark.require_driver("GPX") -def test_ogr2ogr_py_21(script_path): - - try: - os.remove("tmp/testogr2ogr21.gpx") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -f GPX tmp/testogr2ogr21.gpx " - + test_py_scripts.get_data_path("utilities") - + "dataforogr2ogr21.csv " - + '-sql "SELECT name AS route_name, 0 as route_fid FROM dataforogr2ogr21" -nlt POINT -nln route_points', - ) - assert "NAME" in open("tmp/testogr2ogr21.gpx", "rt").read() - os.remove("tmp/testogr2ogr21.gpx") - - -############################################################################### -# Test ogr2ogr when the output driver delays the destination layer defn creation (#3384) - - -@pytest.mark.require_driver("MapInfo File") -@pytest.mark.require_driver("CSV") -def test_ogr2ogr_py_22(script_path): - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - '-f "MapInfo File" tmp/testogr2ogr22.mif ' - + test_py_scripts.get_data_path("utilities") - + "dataforogr2ogr21.csv " - + '-sql "SELECT comment, name FROM dataforogr2ogr21" -nlt POINT', - ) - ds = ogr.Open("tmp/testogr2ogr22.mif") - - assert ds is not None - ds.GetLayer(0).GetLayerDefn() - lyr = ds.GetLayer(0) - feat = lyr.GetNextFeature() - if ( - feat.GetFieldAsString("name") != "NAME" - or feat.GetFieldAsString("comment") != "COMMENT" - ): - print(feat.GetFieldAsString("comment")) - ds.Destroy() - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/testogr2ogr22.mif") - pytest.fail(feat.GetFieldAsString("name")) - - ds.Destroy() - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/testogr2ogr22.mif") - - -############################################################################### -# Same as previous but with -select - - -@pytest.mark.require_driver("MapInfo File") -@pytest.mark.require_driver("CSV") -def test_ogr2ogr_py_23(script_path): - - if test_cli_utilities.get_ogr2ogr_path() is None: - pytest.skip() - - gdaltest.runexternal( - test_cli_utilities.get_ogr2ogr_path() - + ' -f "MapInfo File" tmp/testogr2ogr23.mif ' - + test_py_scripts.get_data_path("utilities") - + "dataforogr2ogr21.csv " - + '-sql "SELECT comment, name FROM dataforogr2ogr21" -select comment,name -nlt POINT' - ) - ds = ogr.Open("tmp/testogr2ogr23.mif") - - assert ds is not None - ds.GetLayer(0).GetLayerDefn() - lyr = ds.GetLayer(0) - feat = lyr.GetNextFeature() - if ( - feat.GetFieldAsString("name") != "NAME" - or feat.GetFieldAsString("comment") != "COMMENT" - ): - print(feat.GetFieldAsString("comment")) - ds.Destroy() - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/testogr2ogr23.mif") - pytest.fail(feat.GetFieldAsString("name")) - - ds.Destroy() - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/testogr2ogr23.mif") - - -############################################################################### -# Test -clipsrc with WKT geometry (#3530) - - -@pytest.mark.require_geos -def test_ogr2ogr_py_24(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -clipsrc "POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"', - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - - assert ds.GetLayer(0).GetExtent() == ( - 479609, - 479764, - 4764629, - 4764817, - ), "unexpected extent" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -clipsrc with clip from external datasource - - -@pytest.mark.require_driver("CSV") -@pytest.mark.require_geos -def test_ogr2ogr_py_25(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - f = open("tmp/clip.csv", "wt") - f.write("foo,WKT\n") - f.write( - 'foo,"POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"\n' - ) - f.close() - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -clipsrc tmp/clip.csv -clipsrcwhere foo='foo'", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - - assert ds.GetLayer(0).GetExtent() == ( - 479609, - 479764, - 4764629, - 4764817, - ), "unexpected extent" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - os.remove("tmp/clip.csv") - - -############################################################################### -# Test -clipdst with WKT geometry (#3530) - - -@pytest.mark.require_geos -def test_ogr2ogr_py_26(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -clipdst "POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"', - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - - assert ds.GetLayer(0).GetExtent() == ( - 479609, - 479764, - 4764629, - 4764817, - ), "unexpected extent" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test -clipdst with clip from external datasource - - -@pytest.mark.require_driver("CSV") -@pytest.mark.require_geos -def test_ogr2ogr_py_27(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - f = open("tmp/clip.csv", "wt") - f.write("foo,WKT\n") - f.write( - 'foo,"POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"\n' - ) - f.close() - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - "-nlt MULTIPOLYGON tmp/poly.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -clipdst tmp/clip.csv -clipdstsql "SELECT * from clip"', - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 - - assert ds.GetLayer(0).GetExtent() == ( - 479609, - 479764, - 4764629, - 4764817, - ), "unexpected extent" - - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - os.remove("tmp/clip.csv") - - -############################################################################### -# Test that -overwrite work if the output file doesn't yet exist (#3825) - - -def test_ogr2ogr_py_31(script_path): - - try: - os.stat("tmp/poly.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -overwrite tmp/poly.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - - ds = ogr.Open("tmp/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ds.Destroy() - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/poly.shp") - - -############################################################################### -# Test that -append/-overwrite to a single-file shapefile work without specifying -nln - - -def test_ogr2ogr_py_32(script_path): - - try: - os.stat("tmp/test_ogr2ogr_32.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_32.shp" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_32.shp " + test_py_scripts.get_data_path("ogr") + "poly.shp", - ) - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -append tmp/test_ogr2ogr_32.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_32.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 20, "-append failed" - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -overwrite tmp/test_ogr2ogr_32.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_32.shp") - assert ( - ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ), "-overwrite failed" - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_32.shp") - - -############################################################################### -# Test -explodecollections - - -@pytest.mark.require_driver("CSV") -def test_ogr2ogr_py_33(script_path): - - try: - os.stat("tmp/test_ogr2ogr_33_src.csv") - ogr.GetDriverByName("CSV").DeleteDataSource("tmp/test_ogr2ogr_33_src.csv") - except OSError: - pass - - try: - os.stat("tmp/test_ogr2ogr_33_dst.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_33_dst.shp" - ) - except OSError: - pass - - f = open("tmp/test_ogr2ogr_33_src.csv", "wt") - f.write("foo,WKT\n") - f.write( - 'bar,"MULTIPOLYGON (((10 10,10 11,11 11,11 10,10 10)),((100 100,100 200,200 200,200 100,100 100),(125 125,175 125,175 175,125 175,125 125)))"\n' - ) - f.write('baz,"POLYGON ((0 0,0 1,1 1,1 0,0 0))"\n') - f.close() - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -explodecollections tmp/test_ogr2ogr_33_dst.shp tmp/test_ogr2ogr_33_src.csv -select foo", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_33_dst.shp") - lyr = ds.GetLayer(0) - assert lyr.GetFeatureCount() == 3, "-explodecollections failed" - - feat = lyr.GetFeature(0) - if feat.GetField("foo") != "bar": - feat.DumpReadable() - pytest.fail() - if ( - feat.GetGeometryRef().ExportToWkt() - != "POLYGON ((10 10,10 11,11 11,11 10,10 10))" - ): - feat.DumpReadable() - pytest.fail() - - feat = lyr.GetFeature(1) - if feat.GetField("foo") != "bar": - feat.DumpReadable() - pytest.fail() - if ( - feat.GetGeometryRef().ExportToWkt() - != "POLYGON ((100 100,100 200,200 200,200 100,100 100),(125 125,175 125,175 175,125 175,125 125))" - ): - feat.DumpReadable() - pytest.fail() - - feat = lyr.GetFeature(2) - if feat.GetField("foo") != "baz": - feat.DumpReadable() - pytest.fail() - if feat.GetGeometryRef().ExportToWkt() != "POLYGON ((0 0,0 1,1 1,1 0,0 0))": - feat.DumpReadable() - pytest.fail() - - ds = None - - ogr.GetDriverByName("CSV").DeleteDataSource("tmp/test_ogr2ogr_33_src.csv") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_33_dst.shp" - ) - - -############################################################################### -# Test 'ogr2ogr someDirThatDoesNotExist src.shp -nln someDirThatDoesNotExist' -# This should result in creating a someDirThatDoesNotExist directory with -# someDirThatDoesNotExist.shp/dbf/shx inside this directory - - -def test_ogr2ogr_py_34(script_path): - - try: - os.stat("tmp/test_ogr2ogr_34_dir") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_34_dir" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_34_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln test_ogr2ogr_34_dir", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_34_dir/test_ogr2ogr_34_dir.shp") - assert ( - ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ), "initial shapefile creation failed" - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -append tmp/test_ogr2ogr_34_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln test_ogr2ogr_34_dir", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_34_dir/test_ogr2ogr_34_dir.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 20, "-append failed" - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -overwrite tmp/test_ogr2ogr_34_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -nln test_ogr2ogr_34_dir", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_34_dir/test_ogr2ogr_34_dir.shp") - assert ( - ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ), "-overwrite failed" - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_34_dir") - - -############################################################################### -# Test 'ogr2ogr someDirThatDoesNotExist src.shp' - - -def test_ogr2ogr_py_35(script_path): - - try: - os.stat("tmp/test_ogr2ogr_35_dir") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_35_dir" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_35_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp ", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_35_dir/poly.shp") - assert ( - ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ), "initial shapefile creation failed" - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -append tmp/test_ogr2ogr_35_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_35_dir/poly.shp") - assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 20, "-append failed" - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -overwrite tmp/test_ogr2ogr_35_dir " - + test_py_scripts.get_data_path("ogr") - + "poly.shp", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_35_dir/poly.shp") - assert ( - ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 - ), "-overwrite failed" - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_35_dir") - - -############################################################################### -# Test ogr2ogr -zfield - - -def test_ogr2ogr_py_36(script_path): - - try: - os.stat("tmp/test_ogr2ogr_36.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_36.shp" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_36.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -zfield EAS_ID", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_36.shp") - feat = ds.GetLayer(0).GetNextFeature() - wkt = feat.GetGeometryRef().ExportToWkt() - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_36.shp") - - assert wkt.find(" 168,") != -1 - - -############################################################################### -# Test 'ogr2ogr someDirThatDoesNotExist.shp dataSourceWithMultipleLayer' - - -def test_ogr2ogr_py_37(script_path): - - try: - os.stat("tmp/test_ogr2ogr_37_dir.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_37_dir.shp" - ) - except OSError: - pass - - try: - os.mkdir("tmp/test_ogr2ogr_37_src") - except OSError: - pass - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.shp", "tmp/test_ogr2ogr_37_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.shx", "tmp/test_ogr2ogr_37_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.dbf", "tmp/test_ogr2ogr_37_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.shp", - "tmp/test_ogr2ogr_37_src", - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.shx", - "tmp/test_ogr2ogr_37_src", - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.dbf", - "tmp/test_ogr2ogr_37_src", - ) - - test_py_scripts.run_py_script( - script_path, "ogr2ogr", " tmp/test_ogr2ogr_37_dir.shp tmp/test_ogr2ogr_37_src" - ) - - ds = ogr.Open("tmp/test_ogr2ogr_37_dir.shp") - assert ds is not None and ds.GetLayerCount() == 2 - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_37_src") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_37_dir.shp" - ) - - -############################################################################### -# Test that we take into account the fields by the where clause when combining -# -select and -where (#4015) - - -def test_ogr2ogr_py_38(script_path): - - try: - os.stat("tmp/test_ogr2ogr_38.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_38.shp" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_38.shp " - + test_py_scripts.get_data_path("ogr") - + 'poly.shp -select AREA -where "EAS_ID = 170"', - ) - - ds = ogr.Open("tmp/test_ogr2ogr_38.shp") - lyr = ds.GetLayer(0) - feat = lyr.GetNextFeature() - assert feat is not None - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_38.shp") - - -############################################################################### -# Test 'ogr2ogr someDirThatDoesNotExist.shp dataSourceWithMultipleLayer -sql "select * from alayer"' (#4268) - - -def test_ogr2ogr_py_39(script_path): - - try: - os.stat("tmp/test_ogr2ogr_39_dir.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_39.shp" - ) - except OSError: - pass - - try: - os.mkdir("tmp/test_ogr2ogr_39_src") - except OSError: - pass - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.shp", "tmp/test_ogr2ogr_39_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.shx", "tmp/test_ogr2ogr_39_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "poly.dbf", "tmp/test_ogr2ogr_39_src" - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.shp", - "tmp/test_ogr2ogr_39_src", - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.shx", - "tmp/test_ogr2ogr_39_src", - ) - shutil.copy( - test_py_scripts.get_data_path("ogr") + "shp/testpoly.dbf", - "tmp/test_ogr2ogr_39_src", - ) - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - ' tmp/test_ogr2ogr_39.shp tmp/test_ogr2ogr_39_src -sql "select * from poly"', - ) - - ds = ogr.Open("tmp/test_ogr2ogr_39.shp") - assert ds is not None and ds.GetLayerCount() == 1 - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_39_src") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_39.shp") - - -############################################################################### -# Test -dim 3 and -dim 2 - - -def test_ogr2ogr_py_43(script_path): - - try: - os.stat("tmp/test_ogr2ogr_43_3d.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_43_3d.shp" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_43_3d.shp " - + test_py_scripts.get_data_path("ogr") - + "poly.shp -dim 3", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_43_3d.shp") - lyr = ds.GetLayerByIndex(0) - assert lyr.GetGeomType() == ogr.wkbPolygon25D - ds = None - - try: - os.stat("tmp/test_ogr2ogr_43_2d.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_43_2d.shp" - ) - except OSError: - pass - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " tmp/test_ogr2ogr_43_2d.shp tmp/test_ogr2ogr_43_3d.shp -dim 2", - ) - - ds = ogr.Open("tmp/test_ogr2ogr_43_2d.shp") - lyr = ds.GetLayerByIndex(0) - assert lyr.GetGeomType() == ogr.wkbPolygon - ds = None - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_43_2d.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/test_ogr2ogr_43_3d.shp") - - -############################################################################### -# Test -nlt PROMOTE_TO_MULTI for polygon/multipolygon - - -@pytest.mark.require_driver("GML") -def test_ogr2ogr_py_44(script_path): - - try: - os.stat("tmp/test_ogr2ogr_44_src.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_44_src.shp" - ) - except OSError: - pass - - if os.path.exists("tmp/test_ogr2ogr_44.gml"): - gdal.Unlink("tmp/test_ogr2ogr_44.gml") - if os.path.exists("tmp/test_ogr2ogr_44.xsd"): - gdal.Unlink("tmp/test_ogr2ogr_44.xsd") - - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource( - "tmp/test_ogr2ogr_44_src.shp" - ) - lyr = ds.CreateLayer("test_ogr2ogr_44_src", geom_type=ogr.wkbPolygon) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry(ogr.CreateGeometryFromWkt("POLYGON((0 0,0 1,1 1,0 0))")) - lyr.CreateFeature(feat) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry( - ogr.CreateGeometryFromWkt( - "MULTIPOLYGON(((0 0,0 1,1 1,0 0)),((10 0,10 1,11 1,10 0)))" - ) - ) - lyr.CreateFeature(feat) - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -f GML -dsco FORMAT=GML2 tmp/test_ogr2ogr_44.gml tmp/test_ogr2ogr_44_src.shp -nlt PROMOTE_TO_MULTI", - ) - - f = open("tmp/test_ogr2ogr_44.xsd") - data = f.read() - f.close() - - assert data.find('type="gml:MultiPolygonPropertyType"') != -1 - - f = open("tmp/test_ogr2ogr_44.gml") - data = f.read() - f.close() - - assert ( - data.find( - "0,0 0,1 1,1 0,0" - ) - != -1 - ) - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_44_src.shp" - ) - os.unlink("tmp/test_ogr2ogr_44.gml") - os.unlink("tmp/test_ogr2ogr_44.xsd") - - -############################################################################### -# Test -nlt PROMOTE_TO_MULTI for polygon/multipolygon - - -@pytest.mark.require_driver("GML") -def test_ogr2ogr_py_45(script_path): - - try: - os.stat("tmp/test_ogr2ogr_45_src.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_45_src.shp" - ) - except OSError: - pass - - if os.path.exists("tmp/test_ogr2ogr_45.gml"): - gdal.Unlink("tmp/test_ogr2ogr_45.gml") - if os.path.exists("tmp/test_ogr2ogr_45.xsd"): - gdal.Unlink("tmp/test_ogr2ogr_45.xsd") - - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource( - "tmp/test_ogr2ogr_45_src.shp" - ) - lyr = ds.CreateLayer("test_ogr2ogr_45_src", geom_type=ogr.wkbPolygon) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry(ogr.CreateGeometryFromWkt("POLYGON((0 0,0 1,1 1,0 0))")) - lyr.CreateFeature(feat) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry( - ogr.CreateGeometryFromWkt( - "MULTIPOLYGON(((0 0,0 1,1 1,0 0)),((10 0,10 1,11 1,10 0)))" - ) - ) - lyr.CreateFeature(feat) - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -f GML -dsco FORMAT=GML2 tmp/test_ogr2ogr_45.gml tmp/test_ogr2ogr_45_src.shp -nlt PROMOTE_TO_MULTI", - ) - - f = open("tmp/test_ogr2ogr_45.xsd") - data = f.read() - f.close() - - assert data.find('type="gml:MultiPolygonPropertyType"') != -1 - - f = open("tmp/test_ogr2ogr_45.gml") - data = f.read() - f.close() - - assert ( - data.find( - "0,0 0,1 1,1 0,0" - ) - != -1 - ) - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_45_src.shp" - ) - os.unlink("tmp/test_ogr2ogr_45.gml") - os.unlink("tmp/test_ogr2ogr_45.xsd") - - -############################################################################### -# Test -nlt PROMOTE_TO_MULTI for linestring/multilinestring - - -@pytest.mark.require_driver("CSV") -def test_ogr2ogr_py_46(script_path): - - try: - os.stat("tmp/test_ogr2ogr_45_src.shp") - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_45_src.shp" - ) - except OSError: - pass - - if os.path.exists("tmp/test_ogr2ogr_46.gml"): - gdal.Unlink("tmp/test_ogr2ogr_46.gml") - if os.path.exists("tmp/test_ogr2ogr_46.xsd"): - gdal.Unlink("tmp/test_ogr2ogr_46.xsd") - - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource( - "tmp/test_ogr2ogr_46_src.shp" - ) - lyr = ds.CreateLayer("test_ogr2ogr_46_src", geom_type=ogr.wkbLineString) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry(ogr.CreateGeometryFromWkt("LINESTRING(0 0,0 1,1 1,0 0)")) - lyr.CreateFeature(feat) - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetGeometry( - ogr.CreateGeometryFromWkt( - "MULTILINESTRING((0 0,0 1,1 1,0 0),(10 0,10 1,11 1,10 0))" - ) - ) - lyr.CreateFeature(feat) - ds = None - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - " -f GML -dsco FORMAT=GML2 tmp/test_ogr2ogr_46.gml tmp/test_ogr2ogr_46_src.shp -nlt PROMOTE_TO_MULTI", - ) - - f = open("tmp/test_ogr2ogr_46.xsd") - data = f.read() - f.close() - - assert data.find('type="gml:MultiLineStringPropertyType"') != -1 - - f = open("tmp/test_ogr2ogr_46.gml") - data = f.read() - f.close() - - assert ( - data.find( - "0,0 0,1 1,1 0,0" - ) - != -1 - ) - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource( - "tmp/test_ogr2ogr_46_src.shp" - ) - gdal.Unlink("tmp/test_ogr2ogr_46.gml") - gdal.Unlink("tmp/test_ogr2ogr_46.xsd") diff --git a/doc/source/api/python_samples.rst b/doc/source/api/python_samples.rst index dbf21bddd958..a2831f63ffaa 100644 --- a/doc/source/api/python_samples.rst +++ b/doc/source/api/python_samples.rst @@ -78,7 +78,6 @@ Python direct ports of c++ programs - :ref:`gdalinfo`: A direct port of apps/gdalinfo.c - :ref:`ogrinfo`: A direct port of apps/ogrinfo.cpp - - :ref:`ogr2ogr`: A direct port of apps/ogr2ogr.cpp - :ref:`gdallocationinfo`: A direct port of apps/gdallocationinfo.cpp Python sample scripts that are now programs diff --git a/swig/python/gdal-utils/osgeo_utils/samples/ogr2ogr.py b/swig/python/gdal-utils/osgeo_utils/samples/ogr2ogr.py deleted file mode 100644 index 6cd4136ec250..000000000000 --- a/swig/python/gdal-utils/osgeo_utils/samples/ogr2ogr.py +++ /dev/null @@ -1,1920 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# ***************************************************************************** -# $Id$ -# -# Project: OpenGIS Simple Features Reference Implementation -# Purpose: Python port of a simple client for translating between formats. -# Author: Even Rouault, -# -# Port from ogr2ogr.cpp whose author is Frank Warmerdam -# -# ***************************************************************************** -# Copyright (c) 2010-2013, Even Rouault -# Copyright (c) 1999, Frank Warmerdam -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -# ************************************************************************** - -# Note : this is the most direct port of ogr2ogr.cpp possible -# It could be made much more Python'ish ! - -import os -import stat -import sys - -from osgeo import gdal, ogr, osr - -############################################################################### - - -class ScaledProgressObject(object): - def __init__(self, mini, maxi, cbk, cbk_data=None): - self.min = mini - self.max = maxi - self.cbk = cbk - self.cbk_data = cbk_data - - -############################################################################### - - -def ScaledProgressFunc(pct, msg, data): - if data.cbk is None: - return True - return data.cbk(data.min + pct * (data.max - data.min), msg, data.cbk_data) - - -############################################################################### - - -def EQUAL(a, b): - return a.lower() == b.lower() - - -############################################################################### -# Redefinition of GDALTermProgress, so that test_ogr2ogr_py.py -# can check that the progress bar is displayed - - -nLastTick = -1 - - -def TermProgress(dfComplete, pszMessage, pProgressArg): - # pylint: disable=unused-argument - - global nLastTick - nThisTick = int(dfComplete * 40.0) - - if nThisTick < 0: - nThisTick = 0 - if nThisTick > 40: - nThisTick = 40 - - # Have we started a new progress run? - if nThisTick < nLastTick and nLastTick >= 39: - nLastTick = -1 - - if nThisTick <= nLastTick: - return True - - while nThisTick > nLastTick: - nLastTick = nLastTick + 1 - if (nLastTick % 4) == 0: - sys.stdout.write("%d" % ((nLastTick / 4) * 10)) - else: - sys.stdout.write(".") - - if nThisTick == 40: - print(" - done.") - else: - sys.stdout.flush() - - return True - - -class TargetLayerInfo(object): - def __init__(self): - self.poDstLayer = None - self.poCT = None - # self.papszTransformOptions = None - self.panMap = None - self.iSrcZField = None - - -class AssociatedLayers(object): - def __init__(self): - self.poSrcLayer = None - self.psInfo = None - - -# ********************************************************************** -# main() -# ********************************************************************** - - -bSkipFailures = False -nGroupTransactions = 200 -bPreserveFID = False -nFIDToFetch = ogr.NullFID - - -class Enum(set): - def __getattr__(self, name): - if name in self: - return name - raise AttributeError - - -GeomOperation = Enum(["NONE", "SEGMENTIZE", "SIMPLIFY_PRESERVE_TOPOLOGY"]) - - -def main(args=None, progress_func=TermProgress, progress_data=None): - - global bSkipFailures - global nGroupTransactions - global bPreserveFID - global nFIDToFetch - - version_num = int(gdal.VersionInfo("VERSION_NUM")) - if version_num < 1800: # because of ogr.GetFieldTypeName - print("ERROR: Python bindings of GDAL 1.8.0 or later required") - return 1 - - pszFormat = "ESRI Shapefile" - pszDataSource = None - pszDestDataSource = None - papszLayers = [] - papszDSCO = [] - papszLCO = [] - bTransform = False - bAppend = False - bUpdate = False - bOverwrite = False - pszOutputSRSDef = None - pszSourceSRSDef = None - poOutputSRS = None - bNullifyOutputSRS = False - poSourceSRS = None - pszNewLayerName = None - pszWHERE = None - poSpatialFilter = None - pszSelect = None - papszSelFields = None - pszSQLStatement = None - eGType = -2 - bPromoteToMulti = False - eGeomOp = GeomOperation.NONE - dfGeomOpParam = 0 - papszFieldTypesToString = [] - bDisplayProgress = False - pfnProgress = None - pProgressArg = None - bClipSrc = False - bWrapDateline = False - poClipSrc = None - pszClipSrcDS = None - pszClipSrcSQL = None - pszClipSrcLayer = None - pszClipSrcWhere = None - poClipDst = None - pszClipDstDS = None - pszClipDstSQL = None - pszClipDstLayer = None - pszClipDstWhere = None - # pszSrcEncoding = None - # pszDstEncoding = None - bWrapDateline = False - bExplodeCollections = False - pszZField = None - nCoordDim = -1 - - if args is None: - args = sys.argv - - args = ogr.GeneralCmdLineProcessor(args) - - # -------------------------------------------------------------------- - # Processing command line arguments. - # -------------------------------------------------------------------- - if args is None: - return 2 - - nArgc = len(args) - - iArg = 1 - while iArg < nArgc: - if EQUAL(args[iArg], "-f") and iArg < nArgc - 1: - iArg = iArg + 1 - pszFormat = args[iArg] - - elif EQUAL(args[iArg], "-dsco") and iArg < nArgc - 1: - iArg = iArg + 1 - papszDSCO.append(args[iArg]) - - elif EQUAL(args[iArg], "-lco") and iArg < nArgc - 1: - iArg = iArg + 1 - papszLCO.append(args[iArg]) - - elif EQUAL(args[iArg], "-preserve_fid"): - bPreserveFID = True - - elif len(args[iArg]) >= 5 and EQUAL(args[iArg][0:5], "-skip"): - bSkipFailures = True - nGroupTransactions = 1 # 2409 - - elif EQUAL(args[iArg], "-append"): - bAppend = True - bUpdate = True - - elif EQUAL(args[iArg], "-overwrite"): - bOverwrite = True - bUpdate = True - - elif EQUAL(args[iArg], "-update"): - bUpdate = True - - elif EQUAL(args[iArg], "-fid") and iArg < nArgc - 1: - iArg = iArg + 1 - nFIDToFetch = int(args[iArg]) - - elif EQUAL(args[iArg], "-sql") and iArg < nArgc - 1: - iArg = iArg + 1 - pszSQLStatement = args[iArg] - - elif EQUAL(args[iArg], "-nln") and iArg < nArgc - 1: - iArg = iArg + 1 - pszNewLayerName = args[iArg] - - elif EQUAL(args[iArg], "-nlt") and iArg < nArgc - 1: - - if EQUAL(args[iArg + 1], "NONE"): - eGType = ogr.wkbNone - elif EQUAL(args[iArg + 1], "GEOMETRY"): - eGType = ogr.wkbUnknown - elif EQUAL(args[iArg + 1], "PROMOTE_TO_MULTI"): - bPromoteToMulti = True - elif EQUAL(args[iArg + 1], "POINT"): - eGType = ogr.wkbPoint - elif EQUAL(args[iArg + 1], "LINESTRING"): - eGType = ogr.wkbLineString - elif EQUAL(args[iArg + 1], "POLYGON"): - eGType = ogr.wkbPolygon - elif EQUAL(args[iArg + 1], "GEOMETRYCOLLECTION"): - eGType = ogr.wkbGeometryCollection - elif EQUAL(args[iArg + 1], "MULTIPOINT"): - eGType = ogr.wkbMultiPoint - elif EQUAL(args[iArg + 1], "MULTILINESTRING"): - eGType = ogr.wkbMultiLineString - elif EQUAL(args[iArg + 1], "MULTIPOLYGON"): - eGType = ogr.wkbMultiPolygon - elif EQUAL(args[iArg + 1], "GEOMETRY25D"): - eGType = ogr.wkbUnknown | ogr.wkb25DBit - elif EQUAL(args[iArg + 1], "POINT25D"): - eGType = ogr.wkbPoint25D - elif EQUAL(args[iArg + 1], "LINESTRING25D"): - eGType = ogr.wkbLineString25D - elif EQUAL(args[iArg + 1], "POLYGON25D"): - eGType = ogr.wkbPolygon25D - elif EQUAL(args[iArg + 1], "GEOMETRYCOLLECTION25D"): - eGType = ogr.wkbGeometryCollection25D - elif EQUAL(args[iArg + 1], "MULTIPOINT25D"): - eGType = ogr.wkbMultiPoint25D - elif EQUAL(args[iArg + 1], "MULTILINESTRING25D"): - eGType = ogr.wkbMultiLineString25D - elif EQUAL(args[iArg + 1], "MULTIPOLYGON25D"): - eGType = ogr.wkbMultiPolygon25D - else: - print("-nlt %s: type not recognised." % args[iArg + 1]) - return 1 - - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-dim") and iArg < nArgc - 1: - - nCoordDim = int(args[iArg + 1]) - if nCoordDim != 2 and nCoordDim != 3: - print("-dim %s: value not handled." % args[iArg + 1]) - return 1 - iArg = iArg + 1 - - elif ( - EQUAL(args[iArg], "-tg") or EQUAL(args[iArg], "-gt") - ) and iArg < nArgc - 1: - iArg = iArg + 1 - nGroupTransactions = int(args[iArg]) - - elif EQUAL(args[iArg], "-s_srs") and iArg < nArgc - 1: - iArg = iArg + 1 - pszSourceSRSDef = args[iArg] - - elif EQUAL(args[iArg], "-a_srs") and iArg < nArgc - 1: - iArg = iArg + 1 - pszOutputSRSDef = args[iArg] - if EQUAL(pszOutputSRSDef, "NULL") or EQUAL(pszOutputSRSDef, "NONE"): - pszOutputSRSDef = None - bNullifyOutputSRS = True - - elif EQUAL(args[iArg], "-t_srs") and iArg < nArgc - 1: - iArg = iArg + 1 - pszOutputSRSDef = args[iArg] - bTransform = True - - elif EQUAL(args[iArg], "-spat") and iArg + 4 < nArgc: - oRing = ogr.Geometry(ogr.wkbLinearRing) - - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - - poSpatialFilter = ogr.Geometry(ogr.wkbPolygon) - poSpatialFilter.AddGeometry(oRing) - iArg = iArg + 4 - - elif EQUAL(args[iArg], "-where") and iArg < nArgc - 1: - iArg = iArg + 1 - pszWHERE = args[iArg] - - elif EQUAL(args[iArg], "-select") and iArg < nArgc - 1: - iArg = iArg + 1 - pszSelect = args[iArg] - if pszSelect.find(",") != -1: - papszSelFields = pszSelect.split(",") - else: - papszSelFields = pszSelect.split(" ") - if papszSelFields[0] == "": - papszSelFields = [] - - elif EQUAL(args[iArg], "-simplify") and iArg < nArgc - 1: - iArg = iArg + 1 - eGeomOp = GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY - dfGeomOpParam = float(args[iArg]) - - elif EQUAL(args[iArg], "-segmentize") and iArg < nArgc - 1: - iArg = iArg + 1 - eGeomOp = GeomOperation.SEGMENTIZE - dfGeomOpParam = float(args[iArg]) - - elif EQUAL(args[iArg], "-fieldTypeToString") and iArg < nArgc - 1: - iArg = iArg + 1 - pszFieldTypeToString = args[iArg] - if pszFieldTypeToString.find(",") != -1: - tokens = pszFieldTypeToString.split(",") - else: - tokens = pszFieldTypeToString.split(" ") - - for token in tokens: - if ( - EQUAL(token, "Integer") - or EQUAL(token, "Real") - or EQUAL(token, "String") - or EQUAL(token, "Date") - or EQUAL(token, "Time") - or EQUAL(token, "DateTime") - or EQUAL(token, "Binary") - or EQUAL(token, "IntegerList") - or EQUAL(token, "RealList") - or EQUAL(token, "StringList") - ): - - papszFieldTypesToString.append(token) - - elif EQUAL(token, "All"): - papszFieldTypesToString = ["All"] - break - - else: - print("Unhandled type for fieldtypeasstring option : %s " % token) - return Usage() - - elif EQUAL(args[iArg], "-progress"): - bDisplayProgress = True - - # elif EQUAL(args[iArg],"-wrapdateline") ) - # { - # bWrapDateline = True; - # } - # - elif EQUAL(args[iArg], "-clipsrc") and iArg < nArgc - 1: - - bClipSrc = True - if IsNumber(args[iArg + 1]) and iArg < nArgc - 4: - oRing = ogr.Geometry(ogr.wkbLinearRing) - - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - - poClipSrc = ogr.Geometry(ogr.wkbPolygon) - poClipSrc.AddGeometry(oRing) - iArg = iArg + 4 - - elif ( - len(args[iArg + 1]) >= 7 and EQUAL(args[iArg + 1][0:7], "POLYGON") - ) or ( - len(args[iArg + 1]) >= 12 - and EQUAL(args[iArg + 1][0:12], "MULTIPOLYGON") - ): - poClipSrc = ogr.CreateGeometryFromWkt(args[iArg + 1]) - if poClipSrc is None: - print( - "FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n" - ) - return Usage() - - iArg = iArg + 1 - - elif EQUAL(args[iArg + 1], "spat_extent"): - iArg = iArg + 1 - - else: - pszClipSrcDS = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipsrcsql") and iArg < nArgc - 1: - pszClipSrcSQL = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipsrclayer") and iArg < nArgc - 1: - pszClipSrcLayer = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipsrcwhere") and iArg < nArgc - 1: - pszClipSrcWhere = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipdst") and iArg < nArgc - 1: - - if IsNumber(args[iArg + 1]) and iArg < nArgc - 4: - oRing = ogr.Geometry(ogr.wkbLinearRing) - - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 4])) - oRing.AddPoint_2D(float(args[iArg + 3]), float(args[iArg + 2])) - oRing.AddPoint_2D(float(args[iArg + 1]), float(args[iArg + 2])) - - poClipDst = ogr.Geometry(ogr.wkbPolygon) - poClipDst.AddGeometry(oRing) - iArg = iArg + 4 - - elif ( - len(args[iArg + 1]) >= 7 and EQUAL(args[iArg + 1][0:7], "POLYGON") - ) or ( - len(args[iArg + 1]) >= 12 - and EQUAL(args[iArg + 1][0:12], "MULTIPOLYGON") - ): - poClipDst = ogr.CreateGeometryFromWkt(args[iArg + 1]) - if poClipDst is None: - print( - "FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n" - ) - return Usage() - - iArg = iArg + 1 - - elif EQUAL(args[iArg + 1], "spat_extent"): - iArg = iArg + 1 - - else: - pszClipDstDS = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipdstsql") and iArg < nArgc - 1: - pszClipDstSQL = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipdstlayer") and iArg < nArgc - 1: - pszClipDstLayer = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-clipdstwhere") and iArg < nArgc - 1: - pszClipDstWhere = args[iArg + 1] - iArg = iArg + 1 - - elif EQUAL(args[iArg], "-explodecollections"): - bExplodeCollections = True - - elif EQUAL(args[iArg], "-zfield") and iArg < nArgc - 1: - pszZField = args[iArg + 1] - iArg = iArg + 1 - - elif args[iArg][0] == "-": - return Usage() - - elif pszDestDataSource is None: - pszDestDataSource = args[iArg] - elif pszDataSource is None: - pszDataSource = args[iArg] - else: - papszLayers.append(args[iArg]) - - iArg = iArg + 1 - - if pszDataSource is None: - return Usage() - - if bPreserveFID and bExplodeCollections: - print( - "FAILURE: cannot use -preserve_fid and -explodecollections at the same time\n\n" - ) - return Usage() - - if bClipSrc and pszClipSrcDS is not None: - poClipSrc = LoadGeometry( - pszClipSrcDS, pszClipSrcSQL, pszClipSrcLayer, pszClipSrcWhere - ) - if poClipSrc is None: - print("FAILURE: cannot load source clip geometry\n") - return Usage() - - elif bClipSrc and poClipSrc is None: - if poSpatialFilter is not None: - poClipSrc = poSpatialFilter.Clone() - if poClipSrc is None: - print( - "FAILURE: -clipsrc must be used with -spat option or a\n" - + "bounding box, WKT string or datasource must be specified\n" - ) - return Usage() - - if pszClipDstDS is not None: - poClipDst = LoadGeometry( - pszClipDstDS, pszClipDstSQL, pszClipDstLayer, pszClipDstWhere - ) - if poClipDst is None: - print("FAILURE: cannot load dest clip geometry\n") - return Usage() - - # -------------------------------------------------------------------- - # Open data source. - # -------------------------------------------------------------------- - poDS = ogr.Open(pszDataSource, False) - - # -------------------------------------------------------------------- - # Report failure - # -------------------------------------------------------------------- - if poDS is None: - print( - "FAILURE:\n" - + "Unable to open datasource `%s' with the following drivers." - % pszDataSource - ) - - for iDriver in range(ogr.GetDriverCount()): - print(" -> " + ogr.GetDriver(iDriver).GetName()) - - return 1 - - # -------------------------------------------------------------------- - # Try opening the output datasource as an existing, writable - # -------------------------------------------------------------------- - poODS = None - poDriver = None - - if bUpdate: - with ogr.ExceptionMgr(useExceptions=False), gdal.quiet_errors(): - poODS = ogr.Open(pszDestDataSource, True) - if poODS is None: - - if bOverwrite or bAppend: - with ogr.ExceptionMgr(useExceptions=False), gdal.quiet_errors(): - poODS = ogr.Open(pszDestDataSource, False) - if poODS is None: - # the datasource doesn't exist at all - bUpdate = False - else: - poODS.delete() - poODS = None - - if bUpdate: - print( - "FAILURE:\n" - + "Unable to open existing output datasource `%s'." - % pszDestDataSource - ) - return 1 - - elif papszDSCO: - print( - "WARNING: Datasource creation options ignored since an existing datasource\n" - + " being updated." - ) - - if poODS is not None: - poDriver = poODS.GetDriver() - - # -------------------------------------------------------------------- - # Find the output driver. - # -------------------------------------------------------------------- - if not bUpdate: - poDriver = ogr.GetDriverByName(pszFormat) - if poDriver is None: - print("Unable to find driver `%s'." % pszFormat) - print("The following drivers are available:") - - for iDriver in range(ogr.GetDriverCount()): - print(" -> %s" % ogr.GetDriver(iDriver).GetName()) - - return 1 - - if not poDriver.TestCapability(ogr.ODrCCreateDataSource): - print("%s driver does not support data source creation." % pszFormat) - return 1 - - # -------------------------------------------------------------------- - # Special case to improve user experience when translating - # a datasource with multiple layers into a shapefile. If the - # user gives a target datasource with .shp and it does not exist, - # the shapefile driver will try to create a file, but this is not - # appropriate because here we have several layers, so create - # a directory instead. - # -------------------------------------------------------------------- - if ( - EQUAL(poDriver.GetName(), "ESRI Shapefile") - and pszSQLStatement is None - and (len(papszLayers) > 1 or (not papszLayers and poDS.GetLayerCount() > 1)) - and pszNewLayerName is None - and EQUAL(os.path.splitext(pszDestDataSource)[1], ".SHP") - ): - - try: - os.stat(pszDestDataSource) - except OSError: - try: - # decimal 493 = octal 0755. Python 3 needs 0o755, but - # this syntax is only supported by Python >= 2.6 - os.mkdir(pszDestDataSource, 493) - except OSError: - print( - "Failed to create directory %s\n" - "for shapefile datastore.\n" % pszDestDataSource - ) - return 1 - - # -------------------------------------------------------------------- - # Create the output data source. - # -------------------------------------------------------------------- - poODS = poDriver.CreateDataSource(pszDestDataSource, options=papszDSCO) - if poODS is None: - print("%s driver failed to create %s" % (pszFormat, pszDestDataSource)) - return 1 - - # -------------------------------------------------------------------- - # Parse the output SRS definition if possible. - # -------------------------------------------------------------------- - if pszOutputSRSDef is not None: - poOutputSRS = osr.SpatialReference() - poOutputSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) - if poOutputSRS.SetFromUserInput(pszOutputSRSDef) != 0: - print("Failed to process SRS definition: %s" % pszOutputSRSDef) - return 1 - - # -------------------------------------------------------------------- - # Parse the source SRS definition if possible. - # -------------------------------------------------------------------- - if pszSourceSRSDef is not None: - poSourceSRS = osr.SpatialReference() - poSourceSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) - if poSourceSRS.SetFromUserInput(pszSourceSRSDef) != 0: - print("Failed to process SRS definition: %s" % pszSourceSRSDef) - return 1 - - # -------------------------------------------------------------------- - # For OSM file. - # -------------------------------------------------------------------- - bSrcIsOSM = poDS.GetDriver() is not None and poDS.GetDriver().GetName() == "OSM" - nSrcFileSize = 0 - if bSrcIsOSM and poDS.GetName() != "/vsistdin/": - sStat = gdal.VSIStatL(poDS.GetName()) - if sStat is not None: - nSrcFileSize = sStat.size - - # -------------------------------------------------------------------- - # Special case for -sql clause. No source layers required. - # -------------------------------------------------------------------- - if pszSQLStatement is not None: - if pszWHERE is not None: - print("-where clause ignored in combination with -sql.") - if papszLayers: - print("layer names ignored in combination with -sql.") - - poResultSet = poDS.ExecuteSQL(pszSQLStatement, poSpatialFilter, None) - - if poResultSet is not None: - nCountLayerFeatures = 0 - if bDisplayProgress: - if bSrcIsOSM: - pfnProgress = progress_func - pProgressArg = progress_data - - elif not poResultSet.TestCapability(ogr.OLCFastFeatureCount): - print("Progress turned off as fast feature count is not available.") - bDisplayProgress = False - - else: - nCountLayerFeatures = poResultSet.GetFeatureCount() - pfnProgress = progress_func - pProgressArg = progress_data - - # -------------------------------------------------------------------- - # Special case to improve user experience when translating into - # single file shapefile and source has only one layer, and that - # the layer name isn't specified - # -------------------------------------------------------------------- - if EQUAL(poDriver.GetName(), "ESRI Shapefile") and pszNewLayerName is None: - try: - mode = os.stat(pszDestDataSource).st_mode - if (mode & stat.S_IFDIR) == 0: - pszNewLayerName = os.path.splitext( - os.path.basename(pszDestDataSource) - )[0] - except OSError: - pass - - psInfo = SetupTargetLayer( - poDS, - poResultSet, - poODS, - papszLCO, - pszNewLayerName, - bTransform, - poOutputSRS, - bNullifyOutputSRS, - poSourceSRS, - papszSelFields, - bAppend, - eGType, - bPromoteToMulti, - nCoordDim, - bOverwrite, - papszFieldTypesToString, - bWrapDateline, - bExplodeCollections, - pszZField, - pszWHERE, - ) - - poResultSet.ResetReading() - - if psInfo is None or not TranslateLayer( - psInfo, - poDS, - poResultSet, - poODS, - poOutputSRS, - bNullifyOutputSRS, - eGType, - bPromoteToMulti, - nCoordDim, - eGeomOp, - dfGeomOpParam, - nCountLayerFeatures, - poClipSrc, - poClipDst, - bExplodeCollections, - nSrcFileSize, - None, - pfnProgress, - pProgressArg, - ): - print( - "Terminating translation prematurely after failed\n" - + "translation from sql statement." - ) - - return 1 - - poDS.ReleaseResultSet(poResultSet) - - # -------------------------------------------------------------------- - # Special case for layer interleaving mode. - # -------------------------------------------------------------------- - elif bSrcIsOSM and gdal.GetConfigOption("OGR_INTERLEAVED_READING", None) is None: - - gdal.SetConfigOption("OGR_INTERLEAVED_READING", "YES") - - # if (bSplitListFields) - # { - # fprintf( stderr, "FAILURE: -splitlistfields not supported in this mode\n" ); - # exit( 1 ); - # } - - nSrcLayerCount = poDS.GetLayerCount() - pasAssocLayers = [AssociatedLayers() for _ in range(nSrcLayerCount)] - - # -------------------------------------------------------------------- - # Special case to improve user experience when translating into - # single file shapefile and source has only one layer, and that - # the layer name isn't specified - # -------------------------------------------------------------------- - - if ( - EQUAL(poDriver.GetName(), "ESRI Shapefile") - and (len(papszLayers) == 1 or nSrcLayerCount == 1) - and pszNewLayerName is None - ): - try: - mode = os.stat(pszDestDataSource).st_mode - if (mode & stat.S_IFDIR) == 0: - pszNewLayerName = os.path.splitext( - os.path.basename(pszDestDataSource) - )[0] - except OSError: - pass - - if bDisplayProgress and bSrcIsOSM: - pfnProgress = progress_func - pProgressArg = progress_data - - # -------------------------------------------------------------------- - # If no target layer specified, use all source layers. - # -------------------------------------------------------------------- - if not papszLayers: - papszLayers = [None] * nSrcLayerCount - for iLayer in range(nSrcLayerCount): - poLayer = poDS.GetLayer(iLayer) - if poLayer is None: - print("FAILURE: Couldn't fetch advertised layer %d!" % iLayer) - return 1 - - papszLayers[iLayer] = poLayer.GetName() - else: - if bSrcIsOSM: - osInterestLayers = "SET interest_layers =" - for iLayer, papszLayer in enumerate(papszLayers): - if iLayer != 0: - osInterestLayers = osInterestLayers + "," - osInterestLayers = osInterestLayers + papszLayer - - poDS.ExecuteSQL(osInterestLayers, None, None) - - # -------------------------------------------------------------------- - # First pass to set filters and create target layers. - # -------------------------------------------------------------------- - for iLayer in range(nSrcLayerCount): - poLayer = poDS.GetLayer(iLayer) - if poLayer is None: - print("FAILURE: Couldn't fetch advertised layer %d!" % iLayer) - return 1 - - pasAssocLayers[iLayer].poSrcLayer = poLayer - - if CSLFindString(papszLayers, poLayer.GetName()) >= 0: - if pszWHERE is not None: - if poLayer.SetAttributeFilter(pszWHERE) != 0: - print( - "FAILURE: SetAttributeFilter(%s) on layer '%s' failed.\n" - % (pszWHERE, poLayer.GetName()) - ) - if not bSkipFailures: - return 1 - - if poSpatialFilter is not None: - poLayer.SetSpatialFilter(poSpatialFilter) - - psInfo = SetupTargetLayer( - poDS, - poLayer, - poODS, - papszLCO, - pszNewLayerName, - bTransform, - poOutputSRS, - bNullifyOutputSRS, - poSourceSRS, - papszSelFields, - bAppend, - eGType, - bPromoteToMulti, - nCoordDim, - bOverwrite, - papszFieldTypesToString, - bWrapDateline, - bExplodeCollections, - pszZField, - pszWHERE, - ) - - if psInfo is None and not bSkipFailures: - return 1 - - pasAssocLayers[iLayer].psInfo = psInfo - else: - pasAssocLayers[iLayer].psInfo = None - - # -------------------------------------------------------------------- - # Second pass to process features in a interleaved layer mode. - # -------------------------------------------------------------------- - bHasLayersNonEmpty = True - while bHasLayersNonEmpty: - bHasLayersNonEmpty = False - - for iLayer in range(nSrcLayerCount): - poLayer = pasAssocLayers[iLayer].poSrcLayer - psInfo = pasAssocLayers[iLayer].psInfo - anReadFeatureCount = [0] - - if psInfo is not None: - if ( - not TranslateLayer( - psInfo, - poDS, - poLayer, - poODS, - poOutputSRS, - bNullifyOutputSRS, - eGType, - bPromoteToMulti, - nCoordDim, - eGeomOp, - dfGeomOpParam, - 0, - poClipSrc, - poClipDst, - bExplodeCollections, - nSrcFileSize, - anReadFeatureCount, - pfnProgress, - pProgressArg, - ) - and not bSkipFailures - ): - print( - "Terminating translation prematurely after failed\n" - + "translation of layer " - + poLayer.GetName() - + " (use -skipfailures to skip errors)" - ) - - return 1 - else: - # No matching target layer : just consumes the features - - poFeature = poLayer.GetNextFeature() - while poFeature is not None: - anReadFeatureCount[0] = anReadFeatureCount[0] + 1 - poFeature = poLayer.GetNextFeature() - - if anReadFeatureCount[0] != 0: - bHasLayersNonEmpty = True - - else: - - nLayerCount = 0 - papoLayers = [] - - # -------------------------------------------------------------------- - # Process each data source layer. - # -------------------------------------------------------------------- - if not papszLayers: - nLayerCount = poDS.GetLayerCount() - papoLayers = [None] * nLayerCount - iLayer = 0 - - for iLayer in range(nLayerCount): - poLayer = poDS.GetLayer(iLayer) - - if poLayer is None: - print("FAILURE: Couldn't fetch advertised layer %d!" % iLayer) - return 1 - - papoLayers[iLayer] = poLayer - iLayer = iLayer + 1 - - # -------------------------------------------------------------------- - # Process specified data source layers. - # -------------------------------------------------------------------- - else: - nLayerCount = len(papszLayers) - papoLayers = [None] * nLayerCount - iLayer = 0 - - for layername in papszLayers: - poLayer = poDS.GetLayerByName(layername) - - if poLayer is None: - print("FAILURE: Couldn't fetch advertised layer %s!" % layername) - return 1 - - papoLayers[iLayer] = poLayer - iLayer = iLayer + 1 - - panLayerCountFeatures = [0] * nLayerCount - nCountLayersFeatures = 0 - nAccCountFeatures = 0 - - # First pass to apply filters and count all features if necessary - for iLayer in range(nLayerCount): - poLayer = papoLayers[iLayer] - - if pszWHERE is not None: - if poLayer.SetAttributeFilter(pszWHERE) != 0: - print("FAILURE: SetAttributeFilter(%s) failed." % pszWHERE) - if not bSkipFailures: - return 1 - - if poSpatialFilter is not None: - poLayer.SetSpatialFilter(poSpatialFilter) - - if bDisplayProgress and not bSrcIsOSM: - if not poLayer.TestCapability(ogr.OLCFastFeatureCount): - print("Progress turned off as fast feature count is not available.") - bDisplayProgress = False - else: - panLayerCountFeatures[iLayer] = poLayer.GetFeatureCount() - nCountLayersFeatures += panLayerCountFeatures[iLayer] - - # Second pass to do the real job - for iLayer in range(nLayerCount): - poLayer = papoLayers[iLayer] - - if bDisplayProgress: - if bSrcIsOSM: - pfnProgress = progress_func - pProgressArg = progress_data - else: - pfnProgress = ScaledProgressFunc - pProgressArg = ScaledProgressObject( - nAccCountFeatures * 1.0 / nCountLayersFeatures, - (nAccCountFeatures + panLayerCountFeatures[iLayer]) - * 1.0 - / nCountLayersFeatures, - progress_func, - progress_data, - ) - - nAccCountFeatures += panLayerCountFeatures[iLayer] - - # -------------------------------------------------------------------- - # Special case to improve user experience when translating into - # single file shapefile and source has only one layer, and that - # the layer name isn't specified - # -------------------------------------------------------------------- - if ( - EQUAL(poDriver.GetName(), "ESRI Shapefile") - and nLayerCount == 1 - and pszNewLayerName is None - ): - try: - mode = os.stat(pszDestDataSource).st_mode - if (mode & stat.S_IFDIR) == 0: - pszNewLayerName = os.path.splitext( - os.path.basename(pszDestDataSource) - )[0] - except OSError: - pass - - psInfo = SetupTargetLayer( - poDS, - poLayer, - poODS, - papszLCO, - pszNewLayerName, - bTransform, - poOutputSRS, - bNullifyOutputSRS, - poSourceSRS, - papszSelFields, - bAppend, - eGType, - bPromoteToMulti, - nCoordDim, - bOverwrite, - papszFieldTypesToString, - bWrapDateline, - bExplodeCollections, - pszZField, - pszWHERE, - ) - - poLayer.ResetReading() - - if ( - psInfo is None - or not TranslateLayer( - psInfo, - poDS, - poLayer, - poODS, - poOutputSRS, - bNullifyOutputSRS, - eGType, - bPromoteToMulti, - nCoordDim, - eGeomOp, - dfGeomOpParam, - panLayerCountFeatures[iLayer], - poClipSrc, - poClipDst, - bExplodeCollections, - nSrcFileSize, - None, - pfnProgress, - pProgressArg, - ) - ) and not bSkipFailures: - print( - "Terminating translation prematurely after failed\n" - + "translation of layer " - + poLayer.GetLayerDefn().GetName() - + " (use -skipfailures to skip errors)" - ) - - return 1 - - # -------------------------------------------------------------------- - # Close down. - # -------------------------------------------------------------------- - # We must explicitly destroy the output dataset in order the file - # to be properly closed ! - poODS.Destroy() - poDS.Destroy() - - return 0 - - -# ********************************************************************** -# Usage() -# ********************************************************************** - - -def Usage(): - - print( - "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n" - + " [-select field_list] [-where restricted_where] \n" - + " [-progress] [-sql ] \n" - + " [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n" - + " [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n" - + " [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n" - + " [-simplify tolerance]\n" - + " [-fieldTypeToString All|(type1[,type2]...)] [-explodecollections] \n" # // " [-segmentize max_dist] [-fieldTypeToString All|(type1[,type2]...)]\n" + \ - + " dst_datasource_name src_datasource_name\n" - + " [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3] [layer [layer ...]]\n" - + "\n" - + " -f format_name: output file format name, possible values are:" - ) - - for iDriver in range(ogr.GetDriverCount()): - poDriver = ogr.GetDriver(iDriver) - - if poDriver.TestCapability(ogr.ODrCCreateDataSource): - print(' -f "' + poDriver.GetName() + '"') - - print( - " -append: Append to existing layer instead of creating new if it exists\n" - + " -overwrite: delete the output layer and recreate it empty\n" - + " -update: Open existing output datasource in update mode\n" - + ' -progress: Display progress on terminal. Only works if input layers have the "fast feature count" capability\n' - + " -select field_list: Comma-delimited list of fields from input layer to\n" - + " copy to the new layer (defaults to all)\n" - + " -where restricted_where: Attribute query (like SQL WHERE)\n" - + " -sql statement: Execute given SQL statement and save result.\n" - + " -skipfailures: skip features or layers that fail to convert\n" - + " -gt n: group n features per transaction (default 200)\n" - + " -spat xmin ymin xmax ymax: spatial query extents\n" - + " -simplify tolerance: distance tolerance for simplification.\n" - + # //" -segmentize max_dist: maximum distance between 2 nodes.\n" + \ - # //" Used to create intermediate points\n" + \ - " -dsco NAME=VALUE: Dataset creation option (format specific)\n" - + " -lco NAME=VALUE: Layer creation option (format specific)\n" - + " -nln name: Assign an alternate name to the new layer\n" - + " -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY,\n" - + " POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n" - + ' MULTIPOLYGON, or MULTILINESTRING. Add "25D" for 3D layers.\n' - + " Default is type of source layer.\n" - + " -dim dimension: Force the coordinate dimension to the specified value.\n" - + " -fieldTypeToString type1,...: Converts fields of specified types to\n" - + " fields of type string in the new layer. Valid types are : \n" - + " Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,\n" - + " StringList. Special value All can be used to convert all fields to strings." - ) - - print( - " -a_srs srs_def: Assign an output SRS\n" - " -t_srs srs_def: Reproject/transform to this SRS on output\n" - " -s_srs srs_def: Override source SRS\n" - "\n" - " Srs_def can be a full WKT definition (hard to escape properly),\n" - " or a well known definition (i.e. EPSG:4326) or a file with a WKT\n" - " definition." - ) - - return 2 - - -def CSLFindString(v, mystr): - i = 0 - for strIter in v: - if EQUAL(strIter, mystr): - return i - i = i + 1 - return -1 - - -def IsNumber(pszStr): - try: - float(pszStr) - return True - except ValueError: - return False - - -def LoadGeometry(pszDS, pszSQL, pszLyr, pszWhere): - poGeom = None - - poDS = ogr.Open(pszDS, False) - if poDS is None: - return None - - if pszSQL is not None: - poLyr = poDS.ExecuteSQL(pszSQL, None, None) - elif pszLyr is not None: - poLyr = poDS.GetLayerByName(pszLyr) - else: - poLyr = poDS.GetLayer(0) - - if poLyr is None: - print("Failed to identify source layer from datasource.") - poDS.Destroy() - return None - - if pszWhere is not None: - poLyr.SetAttributeFilter(pszWhere) - - poFeat = poLyr.GetNextFeature() - while poFeat is not None: - poSrcGeom = poFeat.GetGeometryRef() - if poSrcGeom is not None: - eType = wkbFlatten(poSrcGeom.GetGeometryType()) - - if poGeom is None: - poGeom = ogr.Geometry(ogr.wkbMultiPolygon) - - if eType == ogr.wkbPolygon: - poGeom.AddGeometry(poSrcGeom) - elif eType == ogr.wkbMultiPolygon: - for iGeom in range(poSrcGeom.GetGeometryCount()): - poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom)) - - else: - print("ERROR: Geometry not of polygon type.") - if pszSQL is not None: - poDS.ReleaseResultSet(poLyr) - poDS.Destroy() - return None - - poFeat = poLyr.GetNextFeature() - - if pszSQL is not None: - poDS.ReleaseResultSet(poLyr) - poDS.Destroy() - - return poGeom - - -def wkbFlatten(x): - return x & (~ogr.wkb25DBit) - - -# ********************************************************************** -# SetZ() -# ********************************************************************** - - -def SetZ(poGeom, dfZ): - - if poGeom is None: - return - - eGType = wkbFlatten(poGeom.GetGeometryType()) - if eGType == ogr.wkbPoint: - poGeom.SetPoint(0, poGeom.GetX(), poGeom.GetY(), dfZ) - - elif eGType == ogr.wkbLineString or eGType == ogr.wkbLinearRing: - for i in range(poGeom.GetPointCount()): - poGeom.SetPoint(i, poGeom.GetX(i), poGeom.GetY(i), dfZ) - - elif ( - eGType == ogr.wkbPolygon - or eGType == ogr.wkbMultiPoint - or eGType == ogr.wkbMultiLineString - or eGType == ogr.wkbMultiPolygon - or eGType == ogr.wkbGeometryCollection - ): - for i in range(poGeom.GetGeometryCount()): - SetZ(poGeom.GetGeometryRef(i), dfZ) - - -# ********************************************************************** -# SetupTargetLayer() -# ********************************************************************** - - -def SetupTargetLayer( - poSrcDS, - poSrcLayer, - poDstDS, - papszLCO, - pszNewLayerName, - bTransform, - poOutputSRS, - bNullifyOutputSRS, - poSourceSRS, - papszSelFields, - bAppend, - eGType, - bPromoteToMulti, - nCoordDim, - bOverwrite, - papszFieldTypesToString, - bWrapDateline, - bExplodeCollections, - pszZField, - pszWHERE, -): - # pylint: disable=unused-argument - if pszNewLayerName is None: - pszNewLayerName = poSrcLayer.GetLayerDefn().GetName() - - # -------------------------------------------------------------------- - # Setup coordinate transformation if we need it. - # -------------------------------------------------------------------- - poCT = None - - if bTransform: - if poSourceSRS is None: - poSourceSRS = poSrcLayer.GetSpatialRef() - - if poSourceSRS is None: - print( - "Can't transform coordinates, source layer has no\n" - + "coordinate system. Use -s_srs to set one." - ) - return None - - poCT = osr.CoordinateTransformation(poSourceSRS, poOutputSRS) - if poCT is None: - pszWKT = None - - print( - "Failed to create coordinate transformation between the\n" - + "following coordinate systems. This may be because they\n" - + "are not transformable." - ) - - pszWKT = poSourceSRS.ExportToPrettyWkt(0) - print("Source:\n" + pszWKT) - - pszWKT = poOutputSRS.ExportToPrettyWkt(0) - print("Target:\n" + pszWKT) - return None - - # -------------------------------------------------------------------- - # Get other info. - # -------------------------------------------------------------------- - poSrcFDefn = poSrcLayer.GetLayerDefn() - - if poOutputSRS is None and not bNullifyOutputSRS: - poOutputSRS = poSrcLayer.GetSpatialRef() - - # -------------------------------------------------------------------- - # Find the layer. - # -------------------------------------------------------------------- - - # GetLayerByName() can instantiate layers that would have been - # 'hidden' otherwise, for example, non-spatial tables in a - # PostGIS-enabled database, so this apparently useless command is - # not useless. (#4012) - gdal.PushErrorHandler("CPLQuietErrorHandler") - poDstLayer = poDstDS.GetLayerByName(pszNewLayerName) - gdal.PopErrorHandler() - gdal.ErrorReset() - - iLayer = -1 - if poDstLayer is not None: - nLayerCount = poDstDS.GetLayerCount() - for iLayer in range(nLayerCount): - poLayer = poDstDS.GetLayer(iLayer) - # The .cpp version compares on pointers directly, but we cannot - # do this with swig object, so just compare the names. - if poLayer is not None and poLayer.GetName() == poDstLayer.GetName(): - break - - if iLayer == nLayerCount: - # Shouldn't happen with an ideal driver - poDstLayer = None - - # -------------------------------------------------------------------- - # If the user requested overwrite, and we have the layer in - # question we need to delete it now so it will get recreated - # (overwritten). - # -------------------------------------------------------------------- - if poDstLayer is not None and bOverwrite: - if poDstDS.DeleteLayer(iLayer) != 0: - print("DeleteLayer() failed when overwrite requested.") - return None - - poDstLayer = None - - # -------------------------------------------------------------------- - # If the layer does not exist, then create it. - # -------------------------------------------------------------------- - if poDstLayer is None: - if eGType == -2: - eGType = poSrcFDefn.GetGeomType() - - n25DBit = eGType & ogr.wkb25DBit - if bPromoteToMulti: - if wkbFlatten(eGType) == ogr.wkbLineString: - eGType = ogr.wkbMultiLineString | n25DBit - elif wkbFlatten(eGType) == ogr.wkbPolygon: - eGType = ogr.wkbMultiPolygon | n25DBit - - if bExplodeCollections: - if wkbFlatten(eGType) == ogr.wkbMultiPoint: - eGType = ogr.wkbPoint | n25DBit - elif wkbFlatten(eGType) == ogr.wkbMultiLineString: - eGType = ogr.wkbLineString | n25DBit - elif wkbFlatten(eGType) == ogr.wkbMultiPolygon: - eGType = ogr.wkbPolygon | n25DBit - elif wkbFlatten(eGType) == ogr.wkbGeometryCollection: - eGType = ogr.wkbUnknown | n25DBit - - if pszZField is not None: - eGType = eGType | ogr.wkb25DBit - - if nCoordDim == 2: - eGType = eGType & ~ogr.wkb25DBit - elif nCoordDim == 3: - eGType = eGType | ogr.wkb25DBit - - if not poDstDS.TestCapability(ogr.ODsCCreateLayer): - print( - "Layer " - + pszNewLayerName - + "not found, and CreateLayer not supported by driver." - ) - return None - - gdal.ErrorReset() - - poDstLayer = poDstDS.CreateLayer(pszNewLayerName, poOutputSRS, eGType, papszLCO) - - if poDstLayer is None: - return None - - bAppend = False - - # -------------------------------------------------------------------- - # Otherwise we will append to it, if append was requested. - # -------------------------------------------------------------------- - elif not bAppend: - print( - "FAILED: Layer " - + pszNewLayerName - + "already exists, and -append not specified.\n" - + " Consider using -append, or -overwrite." - ) - return None - else: - if papszLCO: - print( - "WARNING: Layer creation options ignored since an existing layer is\n" - + " being appended to." - ) - - # -------------------------------------------------------------------- - # Add fields. Default to copy all field. - # If only a subset of all fields requested, then output only - # the selected fields, and in the order that they were - # selected. - # -------------------------------------------------------------------- - - # Initialize the index-to-index map to -1's - nSrcFieldCount = poSrcFDefn.GetFieldCount() - panMap = [-1] * nSrcFieldCount - - poDstFDefn = poDstLayer.GetLayerDefn() - - if papszSelFields is not None and not bAppend: - - nDstFieldCount = 0 - if poDstFDefn is not None: - nDstFieldCount = poDstFDefn.GetFieldCount() - - for papszSelField in papszSelFields: - - iSrcField = poSrcFDefn.GetFieldIndex(papszSelField) - if iSrcField >= 0: - poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iSrcField) - oFieldDefn = ogr.FieldDefn( - poSrcFieldDefn.GetNameRef(), poSrcFieldDefn.GetType() - ) - oFieldDefn.SetWidth(poSrcFieldDefn.GetWidth()) - oFieldDefn.SetPrecision(poSrcFieldDefn.GetPrecision()) - - if papszFieldTypesToString is not None and ( - CSLFindString(papszFieldTypesToString, "All") != -1 - or CSLFindString( - papszFieldTypesToString, - ogr.GetFieldTypeName(poSrcFieldDefn.GetType()), - ) - != -1 - ): - - oFieldDefn.SetType(ogr.OFTString) - - # The field may have been already created at layer creation - iDstField = -1 - if poDstFDefn is not None: - iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef()) - if iDstField >= 0: - panMap[iSrcField] = iDstField - elif poDstLayer.CreateField(oFieldDefn) == 0: - # now that we've created a field, GetLayerDefn() won't return NULL - if poDstFDefn is None: - poDstFDefn = poDstLayer.GetLayerDefn() - - # Sanity check : if it fails, the driver is buggy - if ( - poDstFDefn is not None - and poDstFDefn.GetFieldCount() != nDstFieldCount + 1 - ): - print( - "The output driver has claimed to have added the %s field, but it did not!" - % oFieldDefn.GetNameRef() - ) - else: - panMap[iSrcField] = nDstFieldCount - nDstFieldCount = nDstFieldCount + 1 - - else: - print("Field '" + papszSelField + "' not found in source layer.") - if not bSkipFailures: - return None - - # -------------------------------------------------------------------- - # Use SetIgnoredFields() on source layer if available - # -------------------------------------------------------------------- - - # Here we differ from the ogr2ogr.cpp implementation since the OGRFeatureQuery - # isn't mapped to swig. So in that case just don't use SetIgnoredFields() - # to avoid issue raised in #4015 - if poSrcLayer.TestCapability(ogr.OLCIgnoreFields) and pszWHERE is None: - papszIgnoredFields = [] - for iSrcField in range(nSrcFieldCount): - pszFieldName = poSrcFDefn.GetFieldDefn(iSrcField).GetNameRef() - bFieldRequested = False - for papszSelField in papszSelFields: - if EQUAL(pszFieldName, papszSelField): - bFieldRequested = True - break - - if pszZField is not None and EQUAL(pszFieldName, pszZField): - bFieldRequested = True - - # If source field not requested, add it to ignored files list - if not bFieldRequested: - papszIgnoredFields.append(pszFieldName) - - poSrcLayer.SetIgnoredFields(papszIgnoredFields) - - elif not bAppend: - - nDstFieldCount = 0 - if poDstFDefn is not None: - nDstFieldCount = poDstFDefn.GetFieldCount() - - for iField in range(nSrcFieldCount): - - poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iField) - oFieldDefn = ogr.FieldDefn( - poSrcFieldDefn.GetNameRef(), poSrcFieldDefn.GetType() - ) - oFieldDefn.SetWidth(poSrcFieldDefn.GetWidth()) - oFieldDefn.SetPrecision(poSrcFieldDefn.GetPrecision()) - - if papszFieldTypesToString is not None and ( - CSLFindString(papszFieldTypesToString, "All") != -1 - or CSLFindString( - papszFieldTypesToString, - ogr.GetFieldTypeName(poSrcFieldDefn.GetType()), - ) - != -1 - ): - - oFieldDefn.SetType(ogr.OFTString) - - # The field may have been already created at layer creation - iDstField = -1 - if poDstFDefn is not None: - iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef()) - if iDstField >= 0: - panMap[iField] = iDstField - elif poDstLayer.CreateField(oFieldDefn) == 0: - # now that we've created a field, GetLayerDefn() won't return NULL - if poDstFDefn is None: - poDstFDefn = poDstLayer.GetLayerDefn() - - # Sanity check : if it fails, the driver is buggy - if ( - poDstFDefn is not None - and poDstFDefn.GetFieldCount() != nDstFieldCount + 1 - ): - print( - "The output driver has claimed to have added the %s field, but it did not!" - % oFieldDefn.GetNameRef() - ) - else: - panMap[iField] = nDstFieldCount - nDstFieldCount = nDstFieldCount + 1 - - else: - # For an existing layer, build the map by fetching the index in the destination - # layer for each source field - if poDstFDefn is None: - print("poDstFDefn == NULL.\n") - return None - - for iField in range(nSrcFieldCount): - poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iField) - iDstField = poDstFDefn.GetFieldIndex(poSrcFieldDefn.GetNameRef()) - if iDstField >= 0: - panMap[iField] = iDstField - - iSrcZField = -1 - if pszZField is not None: - iSrcZField = poSrcFDefn.GetFieldIndex(pszZField) - - psInfo = TargetLayerInfo() - psInfo.poDstLayer = poDstLayer - psInfo.poCT = poCT - # psInfo.papszTransformOptions = papszTransformOptions - psInfo.panMap = panMap - psInfo.iSrcZField = iSrcZField - - return psInfo - - -# ********************************************************************** -# TranslateLayer() -# ********************************************************************** - - -def TranslateLayer( - psInfo, - poSrcDS, - poSrcLayer, - poDstDS, - poOutputSRS, - bNullifyOutputSRS, - eGType, - bPromoteToMulti, - nCoordDim, - eGeomOp, - dfGeomOpParam, - nCountLayerFeatures, - poClipSrc, - poClipDst, - bExplodeCollections, - nSrcFileSize, - pnReadFeatureCount, - pfnProgress, - pProgressArg, -): - # pylint: disable=unused-argument - bForceToPolygon = False - bForceToMultiPolygon = False - bForceToMultiLineString = False - - poDstLayer = psInfo.poDstLayer - # papszTransformOptions = psInfo.papszTransformOptions - poCT = psInfo.poCT - panMap = psInfo.panMap - iSrcZField = psInfo.iSrcZField - - if poOutputSRS is None and not bNullifyOutputSRS: - poOutputSRS = poSrcLayer.GetSpatialRef() - - if wkbFlatten(eGType) == ogr.wkbPolygon: - bForceToPolygon = True - elif wkbFlatten(eGType) == ogr.wkbMultiPolygon: - bForceToMultiPolygon = True - elif wkbFlatten(eGType) == ogr.wkbMultiLineString: - bForceToMultiLineString = True - - # -------------------------------------------------------------------- - # Transfer features. - # -------------------------------------------------------------------- - nFeaturesInTransaction = 0 - nCount = 0 - - if nGroupTransactions > 0: - poDstLayer.StartTransaction() - - while True: - poDstFeature = None - - if nFIDToFetch != ogr.NullFID: - - # // Only fetch feature on first pass. - if nFeaturesInTransaction == 0: - poFeature = poSrcLayer.GetFeature(nFIDToFetch) - else: - poFeature = None - - else: - poFeature = poSrcLayer.GetNextFeature() - - if poFeature is None: - break - - nParts = 0 - nIters = 1 - if bExplodeCollections: - poSrcGeometry = poFeature.GetGeometryRef() - if poSrcGeometry is not None: - eSrcType = wkbFlatten(poSrcGeometry.GetGeometryType()) - if ( - eSrcType == ogr.wkbMultiPoint - or eSrcType == ogr.wkbMultiLineString - or eSrcType == ogr.wkbMultiPolygon - or eSrcType == ogr.wkbGeometryCollection - ): - nParts = poSrcGeometry.GetGeometryCount() - nIters = nParts - if nIters == 0: - nIters = 1 - - for iPart in range(nIters): - nFeaturesInTransaction = nFeaturesInTransaction + 1 - if nFeaturesInTransaction == nGroupTransactions: - poDstLayer.CommitTransaction() - poDstLayer.StartTransaction() - nFeaturesInTransaction = 0 - - gdal.ErrorReset() - poDstFeature = ogr.Feature(poDstLayer.GetLayerDefn()) - - if poDstFeature.SetFromWithMap(poFeature, 1, panMap) != 0: - - if nGroupTransactions > 0: - poDstLayer.CommitTransaction() - - print( - "Unable to translate feature %d from layer %s" - % (poFeature.GetFID(), poSrcLayer.GetName()) - ) - - return False - - if bPreserveFID: - poDstFeature.SetFID(poFeature.GetFID()) - - poDstGeometry = poDstFeature.GetGeometryRef() - if poDstGeometry is not None: - - if nParts > 0: - # For -explodecollections, extract the iPart(th) of the geometry - poPart = poDstGeometry.GetGeometryRef(iPart).Clone() - poDstFeature.SetGeometryDirectly(poPart) - poDstGeometry = poPart - - if iSrcZField != -1: - SetZ(poDstGeometry, poFeature.GetFieldAsDouble(iSrcZField)) - # This will correct the coordinate dimension to 3 - poDupGeometry = poDstGeometry.Clone() - poDstFeature.SetGeometryDirectly(poDupGeometry) - poDstGeometry = poDupGeometry - - if nCoordDim == 2 or nCoordDim == 3: - poDstGeometry.SetCoordinateDimension(nCoordDim) - - if eGeomOp == GeomOperation.SEGMENTIZE: - pass - # if (poDstFeature.GetGeometryRef() is not None and dfGeomOpParam > 0) - # poDstFeature.GetGeometryRef().segmentize(dfGeomOpParam); - elif ( - eGeomOp == GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY - and dfGeomOpParam > 0 - ): - poNewGeom = poDstGeometry.SimplifyPreserveTopology(dfGeomOpParam) - if poNewGeom is not None: - poDstFeature.SetGeometryDirectly(poNewGeom) - poDstGeometry = poNewGeom - - if poClipSrc is not None: - poClipped = poDstGeometry.Intersection(poClipSrc) - if poClipped is None or poClipped.IsEmpty(): - # Report progress - nCount = nCount + 1 - if pfnProgress is not None: - pfnProgress( - nCount * 1.0 / nCountLayerFeatures, "", pProgressArg - ) - continue - - poDstFeature.SetGeometryDirectly(poClipped) - poDstGeometry = poClipped - - if poCT is not None: - eErr = poDstGeometry.Transform(poCT) - if eErr != 0: - if nGroupTransactions > 0: - poDstLayer.CommitTransaction() - - print( - "Failed to reproject feature %d (geometry probably out of source or destination SRS)." - % poFeature.GetFID() - ) - if not bSkipFailures: - return False - - elif poOutputSRS is not None: - poDstGeometry.AssignSpatialReference(poOutputSRS) - - if poClipDst is not None: - poClipped = poDstGeometry.Intersection(poClipDst) - if poClipped is None or poClipped.IsEmpty(): - continue - - poDstFeature.SetGeometryDirectly(poClipped) - poDstGeometry = poClipped - - if bForceToPolygon: - poDstFeature.SetGeometryDirectly(ogr.ForceToPolygon(poDstGeometry)) - - elif bForceToMultiPolygon or ( - bPromoteToMulti - and wkbFlatten(poDstGeometry.GetGeometryType()) == ogr.wkbPolygon - ): - poDstFeature.SetGeometryDirectly( - ogr.ForceToMultiPolygon(poDstGeometry) - ) - - elif bForceToMultiLineString or ( - bPromoteToMulti - and wkbFlatten(poDstGeometry.GetGeometryType()) == ogr.wkbLineString - ): - poDstFeature.SetGeometryDirectly( - ogr.ForceToMultiLineString(poDstGeometry) - ) - - gdal.ErrorReset() - if poDstLayer.CreateFeature(poDstFeature) != 0 and not bSkipFailures: - if nGroupTransactions > 0: - poDstLayer.RollbackTransaction() - - return False - - # Report progress - nCount = nCount + 1 - if pfnProgress is not None: - if nSrcFileSize != 0: - if (nCount % 1000) == 0: - poFCLayer = poSrcDS.ExecuteSQL("GetBytesRead()", None, None) - if poFCLayer is not None: - poFeat = poFCLayer.GetNextFeature() - if poFeat is not None: - pszReadSize = poFeat.GetFieldAsString(0) - nReadSize = int(pszReadSize) - pfnProgress( - nReadSize * 1.0 / nSrcFileSize, "", pProgressArg - ) - poSrcDS.ReleaseResultSet(poFCLayer) - else: - pfnProgress(nCount * 1.0 / nCountLayerFeatures, "", pProgressArg) - - if pnReadFeatureCount is not None: - pnReadFeatureCount[0] = nCount - - if nGroupTransactions > 0: - poDstLayer.CommitTransaction() - - return True - - -if __name__ == "__main__": - sys.exit(main(sys.argv))