Skip to content

Instantly share code, notes, and snippets.

@smellman
Created December 3, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smellman/a2770f3a6c4717315cc0a4538b985a1c to your computer and use it in GitHub Desktop.
Save smellman/a2770f3a6c4717315cc0a4538b985a1c to your computer and use it in GitHub Desktop.
Get BBOX value from poly file
import re
def parse(input_file):
f = open(input_file)
lines = f.readlines()
f.close()
longitudes = []
latitudes = []
for line in lines:
r = re.match("\s+([\w\.\+]+)\s+([\w\.\+]+)", line)
if r:
longitudes.append(float(r.groups()[0]))
latitudes.append(float(r.groups()[1]))
print "BBOX=" + str(min(longitudes)) + "," + str(min(latitudes)) + "," + str(max(longitudes)) + "," + str(max(latitudes))
if __name__ == '__main__':
import argparse
import os
p = argparse.ArgumentParser(description='Get BBOX value from poly file')
p.add_argument("input_file", metavar="input_file", help="Input file")
args = p.parse_args()
input_file = os.path.abspath(args.input_file)
parse(input_file)
@erwanlpfr
Copy link

erwanlpfr commented Sep 22, 2023

Hello,

Thanks for this script.
At some point, your regex is wrong to catch these values :

# poly.file from geofabrik
   -2.959590E+00   3.531872E+01
   -2.946335E+00   3.532466E+01

I would change it like that :

import re

def parse(input_file):
  f = open(input_file)
  lines = f.readlines()
  f.close()
  longitudes = []
  latitudes = []
  for line in lines:
    r = re.match("^\s+([\w\.\+\-]+)\s+([\w\.\+\-]+)", line)

    if r:
      
      longitude = r.groups()[0]
      latitude = r.groups()[1]

      print(longitude, float(longitude), latitude, float(latitude))

      longitudes.append(float(longitude))
      latitudes.append(float(latitude))

  bbox = str(min(longitudes)) + "," + str(min(latitudes)) + "," + str(max(longitudes)) + "," + str(max(latitudes))

  print(bbox)

if __name__ == '__main__':
    import argparse
    import os
    p = argparse.ArgumentParser(description='Get BBOX value from poly file')
    p.add_argument("input_file", metavar="input_file", help="Input file")
    args = p.parse_args()
    input_file = os.path.abspath(args.input_file)
    parse(input_file)

@smellman
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment