python - Filter coordinates in a given range -


i've got hundreds of .out files geographical positions, bulk import sqlite database. however, save time, import line geographical coordinates inside intervals.

the file this:

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

so isn't inside several latitude, , longitude intervals should deleted file.

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

i have included pseudocode show effort, how in bash, awk, python or ever method fastest.

the longitude , latitude third , fourth value here. have 21 latitude intervals, f.ex 69.41 70.95 (latitude).

example input

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; 

desired output latitude interval 69.41 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; 

note preferrably should either written new file or overwrite existing file.

if have 1 interval check, pass them , 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 indicate column of latitude, since changing in edits. note fields have comma separate decimals, replacing them 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 happen have many min , max values, pass them string , slice them, can check against them being in 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);          (i=1;i<=n;i++)  {              if ($lat>=minlat[i] && $lat<=maxlat[i])                {print; next}         }        }' file 

this reads intervals array minlat[] , maxlat[] , compares latitude pairs (minlat[1], maxlat[1]), (minlat[2], maxlat[2]), .... if 1 matches, prints record , skips next one, prevent printing more 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); (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; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -