0

给定一个工作生成文件,它将世界地图裁剪到特定国家的边界​​框。

# boxing: 
INDIA_crop.tif: ETOPO1_Ice_g_geotiff.tif
    gdal_translate -projwin 67.0 37.5 99.0 05.0 ETOPO1_Ice_g_geotiff.tif INDIA_crop.tif
    # ulx uly lrx lry  // W N E S
# unzip:
ETOPO1_Ice_g_geotiff.tif: ETOPO1.zip
    unzip ETOPO1.zip
    touch ETOPO1_Ice_g_geotiff.tif
# download:
ETOPO1.zip:
    curl -o ETOPO1.zip 'http://www.ngdc.noaa.gov/mgg/global/relief/ETOPO1/data/ice_surface/grid_registered/georeferenced_tiff/ETOPO1_Ice_g_geotiff.zip'
clean:
    rm `ls | grep -v 'zip' | grep -v 'Makefile'`

鉴于我目前每次都必须通过手动编辑生成文件来更改此生成文件以进行更改:

1. the country name, 
2. its North border geocoordinate, 
3. its South border geocoordinate, 
4. its East border geocoordinate, 
5. its West border geocoordinate.

鉴于我也有所有国家的数据集,例如:

   data = [  
    { "W":-62.70; "S":-27.55;"E": -54.31; "N":-19.35; "item":"Paraguay"  },
    { "W": 50.71; "S": 24.55;"E":  51.58; "N": 26.11; "item":"Qatar"     },
    { "W": 20.22; "S": 43.69;"E":  29.61; "N": 48.22; "item":"Romania"   }, 
    { "W": 19.64; "S": 41.15;"E":-169.92; "N": 81.25; "item":"Russia"    }, 
    { "W": 29.00; "S": -2.93;"E":  30.80; "N": -1.14; "item":"Rwanda"    },
    { "W": 34.62; "S": 16.33;"E":  55.64; "N": 32.15; "item":"Saudi Arabia"}
    ];

如何循环每行数据以便将参数设置到我的 makefile 中?所以我一次输出所有COUNTRYNAME_crop.tif带有正确边界框的文件。

4

1 回答 1

1

假设您使用的是 GNU make,在我看来,这对于自动生成的 makefile 来说是一个完美的问题。在 make 读取它的 makefile 之后,它将测试每个文件,就好像它是一个目标一样,看看它是否可以重建。如果是这样,并且它被重建,make 将自动重新执行自身。这是一种非常强大的元编程类型。我会将它与递归变量命名结合起来。

1.数据:假设您的数据集是dataset.out这样的:

[  
    { "W":-62.70; "S":-27.55;"E": -54.31; "N":-19.35; "item":"Paraguay"  },
    { "W": 50.71; "S": 24.55;"E":  51.58; "N": 26.11; "item":"Qatar"     },
    { "W": 20.22; "S": 43.69;"E":  29.61; "N": 48.22; "item":"Romania"   }, 
    { "W": 19.64; "S": 41.15;"E":-169.92; "N": 81.25; "item":"Russia"    }, 
    { "W": 29.00; "S": -2.93;"E":  30.80; "N": -1.14; "item":"Rwanda"    },
    { "W": 34.62; "S": 16.33;"E":  55.64; "N": 32.15; "item":"Saudi Arabia"}
];

2.转换器:现在您需要编写实用程序convert-to-makefile。我自己会用 Perl 编写它,但新来的孩子可能会选择 Python。任何。无论如何,对于每个国家,输出应该是这样的:

COUNTRIES += <countryname>
<countryname>-NORTH := <north-coord>
<countryname>-SOUTH := <south-coord>
<countryname>-EAST  := <east-coord>
<countryname>-WEST  := <west-coord>

因此bounding.mk,在生成之后,每个国家都有这些节之一。

3a。Makefile:然后,将其添加到 makefile 的开头:

-include bounding.mk

3b。然后将此规则添加到您的 makefile 的末尾:

bounding.mk: dataset.out
         convert-to-makefile $< > $@

3c。然后你可以这样写你的规则:

all: $(COUNTRIES:%=%_crop.tif)

%_crop.tif: ETOPO1_Ice_g_geotiff.tif
        gdal_translate -projwin $($*-WEST) $($*-NORTH) $($*-EAST) $($*-SOUTH) $< $@

那应该做吧!

于 2013-09-19T14:53:25.787 回答