1

I'm attempting to crop a raster with gdal warp using python bindings, the code works fine as I use it to crop a raster approximately 3k times, of which a couple fail with the following error and produce no outputs:

gdal warp cutline invalid

So following some online suggestions I added the following line of code:

gdal.SetConfigOption("GDALWARP_IGNORE_BAD_CUTLINE", "YES")

which then changes the error I get to the following:

gdal Points of LinearRing do not form a closed linestring

So from this I can see that the polygons that I generated from a linestring then buffered using ogr aren't correct. I've then gone and extracted the geometry as a WkT so I can check them, I've included the full list at this URL: https://pastebin.com/d60L3nni .

Now I'm still stuck because the first and last point are at exactly the same coordinates:

-1406502.73228006 6411785.18363632

and

-1406502.73228006 6411785.18363632

Sure because the first and last points are on the same coordinates then it should be a valid polygon? I've tried passing this throught ArcMap Repair Geometry and it also passes the geometry.IsValid() check in ogr.

4
  • The geometry is valid but it is quite large. How do you pass it to GDAL? Could it be possible that the input gets truncated because of too long string? – user30184 Dec 3 '19 at 15:43
  • It's a two step process, first the linestring is buffered into a polygon and saved to a shapefile, this shapefile is then passed directly to gdal warp. – Francis Dec 3 '19 at 16:25
  • I put your coordinates into WKT POLYGON, converted it into shapefile and used it as cutline for gdalwarp. gdalwarp test.tif out.tif -cutline cutline.shp. I did not get any errors. GDAL version is 3.0.2. – user30184 Dec 3 '19 at 17:18
  • I've upgraded to 3.0.2 as well and I can confirm that has fixed it for me! If you want to add that as an answer I'd be happy to accept it. Unfortunately I have other issues with 3.0.2 and pyinstaller at the moment so I'll have to ask another question as well. – Francis Dec 3 '19 at 17:33
1

With GDAL 3.0.2 gdalwarp works for me with command

gdalwarp test.tif out.tif -cutline cutline.shp

where cutline.shp contains your polygon

POLYGON (( -1406502.73228006 6411785.18363632, -1406502.71096238 6411785.34556016, -1406464.54800294 6411991.25446327, -1406415.66090516 6412194.88388041, -1406356.18366518 6412395.6756775, -1406286.27930599 6412593.07949818, -1406206.13943057 6412786.5542723, -1406115.98369675 6412975.56969896, -1406016.05921514 6413159.6077, -1405906.63987178 6413338.16384006, -1405788.0255775 6413510.74870917, -1405725.47457797 6413592.26675885, -1405716.05921514 6413609.6077, -1405606.63987178 6413788.16384006, -1405488.0255775 6413960.74870917, -1405360.54144583 6414126.8892642, -1405224.53690191 6414286.13012544, -1405080.38472475 6414438.03482475, -1405050.38472475 6414468.03482475, -1404898.48002544 6414612.18700191, -1404739.2391642 6414748.19154583, -1404593.2459549 6414860.21607539, -1404538.48002544 6414912.18700191, -1404379.2391642 6415048.19154583, -1404213.09860917 6415175.6756775, -1404040.51374006 6415294.28997178, -1403861.9576 6415403.70931514, -1403837.68032092 6415416.89080218, -1403710.51374006 6415504.28997178, -1403531.9576 6415613.70931514, -1403347.91959896 6415713.63379675, -1403158.9041723 6415803.78953057, -1402965.42939818 6415883.92940599, -1402768.0255775 6415953.83376518, -1402567.23378041 6416013.31100516, -1402363.60436327 6416062.19810294, -1402157.69546016 6416100.36106238, -1401950.07145307 6416127.69528147, -1401741.30142497 6416144.12583902, -1401531.9576 6416149.6077, -1401322.61377503 6416144.12583902, -1401113.84374693 6416127.69528147, -1400906.21973984 6416100.36106238, -1400700.31083673 6416062.19810294, -1400496.68141959 6416013.31100516, -1400295.8896225 6415953.83376518, -1400098.48580182 6415883.92940599, -1399905.0110277 6415803.78953057, -1399715.99560104 6415713.63379675, -1399531.9576 6415613.70931514, -1399353.40145994 6415504.28997178, -1399180.81659083 6415385.6756775, -1399145.16776086 6415358.32136818, -1399040.99560104 6415308.63379675, -1398856.9576 6415208.70931514, -1398678.40145994 6415099.28997178, -1398505.81659083 6414980.6756775, -1398339.6760358 6414853.19154583, -1398180.43517456 6414717.18700191, -1398028.53047525 6414573.03482475, -1397884.37829809 6414421.13012544, -1397748.37375417 6414261.8892642, -1397620.8896225 6414095.74870917, -1397502.27532822 6413923.16384006, -1397392.85598486 6413744.6077, -1397292.93150325 6413560.56969896, -1397202.77576943 6413371.5542723, -1397122.63589401 6413178.07949818, -1397052.73153482 6412980.6756775, -1396993.25429484 6412779.88388041, -1396944.36719706 6412576.25446327, -1396906.20423762 6412370.34556016, -1396878.87001853 6412162.72155307, -1396862.43946098 6411953.95152497, -1396856.9576 6411744.6077, -1396862.43946098 6411535.26387503, -1396878.87001853 6411326.49384693, -1396906.20423762 6411118.86983984, -1396944.36719706 6410912.96093673, -1396993.25429484 6410709.33151959, -1397052.73153482 6410508.5397225, -1397122.63589401 6410311.13590182, -1397202.77576943 6410117.6611277, -1397292.93150325 6409928.64570104, -1397392.85598486 6409744.6077, -1397502.27532822 6409566.05155994, -1397620.8896225 6409393.46669083, -1397748.37375417 6409227.3261358, -1397884.37829809 6409068.08527456, -1398028.53047525 6408916.18057525, -1398073.53047525 6408871.18057525, -1398225.43517456 6408727.02839809, -1398384.6760358 6408591.02385417, -1398550.81659083 6408463.5397225, -1398723.40145994 6408344.92542822, -1398901.9576 6408235.50608486, -1399085.99560104 6408135.58160325, -1399275.0110277 6408045.42586943, -1399468.48580182 6407965.28599401, -1399665.8896225 6407895.38163482, -1399866.68141959 6407835.90439484, -1400070.31083673 6407787.01729706, -1400276.21973984 6407748.85433762, -1400381.68027296 6407734.97019504, -1400387.04106005 6407731.28582814, -1400565.59720012 6407621.86648479, -1400749.63520116 6407521.94200318, -1400938.65062782 6407431.78626937, -1401132.12540195 6407351.64639396, -1401329.52922263 6407281.74203478, -1401530.32101972 6407222.26479481, -1401733.95043686 6407173.37769704, -1401939.85933997 6407135.2147376, -1402147.48334707 6407107.88051851, -1402356.25337516 6407091.44996097, -1402565.59720014 6407085.9681, -1402774.94102511 6407091.44996099, -1402983.71105321 6407107.88051854, -1403191.3350603 6407135.21473764, -1403397.2439634 6407173.37769709, -1403600.87338054 6407222.26479488, -1403801.66517763 6407281.74203486, -1403999.06899831 6407351.64639406, -1404192.54377243 6407431.78626949, -1404381.55919908 6407521.94200331, -1404565.59720012 6407621.86648493, -1404744.15334017 6407731.28582829, -1404916.73820928 6407849.90012258, -1405082.87876431 6407977.38425426, -1405242.11962554 6408113.38879818, -1405394.02432484 6408257.54097535, -1405538.176502 6408409.44567467, -1405674.18104591 6408568.68653591, -1405801.66517758 6408734.82709094, -1405920.27947186 6408907.41196005, -1406029.69881521 6409085.96810012, -1406129.62329682 6409270.00610116, -1406219.77903063 6409459.02152782, -1406299.91890604 6409652.49630195, -1406369.82326522 6409849.90012263, -1406429.30050519 6410050.69191972, -1406478.18760296 6410254.32133686, -1406516.3505624 6410460.23023997, -1406543.68478149 6410667.85424707, -1406560.11533903 6410876.62427516, -1406565.5972 6411085.96810014, -1406560.11533901 6411295.31192511, -1406543.68478146 6411504.08195321, -1406516.35056236 6411711.7059603, -1406502.73228006 6411785.18363632 ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.