Skip to content Skip to sidebar Skip to footer

Filter Coordinates In A Given Range

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 c

Solution 1:

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.

Test

$ 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

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.

Test

$ 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;

Post a Comment for "Filter Coordinates In A Given Range"