过滤器在给定范围内的坐标 [英] Filter coordinates in a given range

查看:104
本文介绍了过滤器在给定范围内的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数以百计的.OUT文件与地理位置,我会批量导入到SQLite数据库。然而,为了节省时间,我只会用一些时间间隔内地理坐标导入就行了。

I've got hundreds of .out files with geographical positions, that I will bulk import into an SQLite database. However, to save time, I will only import the line with geographical coordinates inside some intervals.

该文件是这样的:

value;value;longitude;latitude;value;value
value;value;longitude;latitude;value;value

所以,一切,是不是里面的若干个的纬度,经度间隔应该从文件中删除。

So everything that isn't inside several latitude, and longitude intervals should be deleted from the file.

for f in *.out
do
for each line in $f:
    if not longitude >= longitude1 and longitude <= longitude2 
    or longitude >= longitude3 and longitude <= longitude4 or
    longitude>=longitude5 and longitude<=4:
         delete line

我已经包括一个伪code表现出一定的努力,但我会怎么做这猛砸时,awk,Python或什么都的方法是最快的。

I have included a pseudocode to show some effort, but how would I do this in Bash, awk, python or what ever method is the fastest.

的经度和纬度是这里的第三和第四值。我有21个纬度的间隔,f.ex 69.41至70.95(纬度)。

The longitude and latitude is the third and fourth value here. I have 21 latitude intervals, f.ex 69.41 to 70.95 (latitude).

输入示例

63;543534;34,12;59,43;22,80;654,324;139543;
63;25725;5,11;59,43;22,80;36,00;1391212800;
61;5382189;3,66;60,93;68,00;158,00;1391212800;
43;25977000;10,72;67,51;170,70;168,00;1391212800;
61;2000;4,54;60,00;352,50;352,00;1391212800;
53;2504210;6,96;62,89;289,40;511,00;1391212800;
27;2594800;22,35;70,24;14,50;98,00;1391212800;
61;257900;5,13;60,13;321,10;195,00;1391212800;
31;2598;18,76;69,56;230,20;235,00;1391212800;
63;44000;5,84;59,01;226,90;227,00;1391212800;
61;0;4,96;60,57;125,50;129,00;1391212800;
57;2575000;4,88;61,77;113,00;276,00;1391212800;
34;258500;16,58;69,70;18,20;201,00;1391212800;
243;217000;7,18;65,25;283,00;145,00;1391212800;
243;21900;7,20;64,97;44,80;109,00;1391212800;
243;2190516;2,44;58,20;270,50;121,00;1391212800;
243;22000;1,94;58,39;305,20;130,00;1391212800;
243;231067000;1,87;58,09;12,00;122,00;1391212800;
243;311000150;3,54;61,13;166,30;332,00;1391212800;
243;257282000;7,21;64,97;267,10;112,00;1391212800;
243;232758000;1,77;61,43;333,30;337,00;1391212800;
27;231711000;22,42;70,27;99,20;99,00;1391212800;
68;231770000;10,06;58,74;5,40;10,00;1391212800;

所需的输出随纬度区间69.41至70.95:

Desired output with latitude interval 69.41 to 70.95:

27;2594800;22,35;70,24;14,50;98,00;1391212800;
31;2598;18,76;69,56;230,20;235,00;1391212800;
34;258500;16,58;69,70;18,20;201,00;1391212800;
27;231711000;22,42;70,27;99,20;99,00;1391212800;

请注意,这preferrably应该是写入到一个新文件或覆盖现有文件。

Note that this preferrably should be either written to a new file or overwrite the existing file.

推荐答案

如果你只是有一个时间间隔检查,通过他们和比较:

If you just have one interval to check, pass them and compare:

awk -v lat=5 -v min_lat=69.41 -v max_lat=70.95 '
        BEGIN {FS=OFS=";"} 
        {sub(",",".",$lat)} 
        $lat>=min_lat && $lat<=max_lat' file

使用经纬度我表示纬度列,因为它是在你编辑的变化。还要注意的领域有一个逗号分隔小数,所以我有一个圆点代替它们。

With lat I indicate the column of the latitude, since it is changing in your edits. Note also the fields have a comma to separate the decimals, so I am replacing them with a dot.

$ awk -v lat=5 -v min_lat=69.41 -v max_lat=70.95 'BEGIN {FS=OFS=";"} {sub(",",".",$lat)} $lat>=min_lat && $lat<=max_lat' file
27;1;2594800;22,35;70.24;14,50;98,00;1391212800;
31;3;2598;18,76;69.56;230,20;235,00;1391212800;
34;3;258500;16,58;69.70;18,20;201,00;1391212800;
27;1;231711000;22,42;70.27;99,20;99,00;1391212800;


如果你碰巧有许多最小值和最大值,它们作为一个字符串传递和切片他们,这样就可以检查对他们在一个数组是:


If you happen to have many min and max values, pass them as a string and slice them, so that you can check against them being in an array:

awk -v lat=4 -v min="69.41 70.39" -v max="70.95 70.86" '
       BEGIN {FS=OFS=";"; n=split(min,minlat," "); m=split(max,maxlat," ")} 
       {sub(",",".",$lat); 
        for (i=1;i<=n;i++)  {
             if ($lat>=minlat[i] && $lat<=maxlat[i])
               {print; next}
        }
       }' file

这读取间隔成阵 minlat [] maxlat [] ,然后纬度与所有比较在对(minlat [1],maxlat [1]),(minlat [2],maxlat [2]),... 。如果有一个相匹配,它打印的记录,并跳到下一个,以prevent打印一次以上。

This reads the intervals into the array minlat[] and maxlat[] and then compares the latitude with all the pairs (minlat[1], maxlat[1]), (minlat[2], maxlat[2]), .... If one matches, it prints the record and skips to the next one, to prevent printing more than once.

$ awk -v lat=4 -v min="69.41 70.39" -v max="70.95 70.86" 'BEGIN {FS=OFS=";"; n=split(min,minlat," "); m=split(max,maxlat," ")} {sub(",",".",$lat); for (i=1;i<=n;i++) {if ($lat>=minlat[i] && $lat<=maxlat[i]) {print; next}}}' file
27;2594800;22,35;70.24;14,50;98,00;1391212800;
31;2598;18,76;69.56;230,20;235,00;1391212800;
34;258500;16,58;69.70;18,20;201,00;1391212800;
27;231711000;22,42;70.27;99,20;99,00;1391212800;

这篇关于过滤器在给定范围内的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆