Initial import
[mirror/userdir-ldap.git] / ud-xearth
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # Generate an xearth database from the LDAP entries
4 # LDAP entires for lat/long can be in one of 3 different formats
5 #    1) Decimal Degrees
6 #        +-DDD.DDDDDDDDDDDDDDD
7 #    2) Degrees Minutes (DGM), common output from GPS units
8 #        +-DDDMM.MMMMMMMMMMMMM
9 #    3) Degrees Minutes Seconds (DGMS)
10 #        +-DDDMMSS.SSSSSSSSSSS
11 # Decimal Degrees is the most basic format, but to have good accuracy it
12 # needs a large number of decimals. The other formats are all derived from it:
13 #  DGM -> DD   DDD + (MM.MMMMMMMM)/60
14 #  DGMS -> DD  DDD + (MM + (SS.SSSSSS)/60)/60
15 # For Latitude + is North, for Longitude + is East
16
17 import string, re, time, ldap, getopt, sys, pwd, posix;
18 from userdir_ldap import *;
19
20 # This needs to check for leading 0 to disambiguate some things
21 def DecDegree(Attr,Type):
22   Parts = re.match('[+-]?(\d*)\\.?(\d*)?',GetAttr(Attr,Type)).groups();
23   Val = string.atof(GetAttr(Attr,Type));
24
25   if (abs(Val) >= 1806060.0):
26      raise ValueError,"Too Big";
27
28   # Val is in DGMS
29   if abs(Val) >= 18060.0 or len(Parts[0]) > 5:
30      Val = Val/100.0;
31      Secs = Val - long(Val);
32      Val = long(Val)/100.0;
33      Min = Val - long(Val);
34      Val = long(Val) + (Min*100.0 + Secs*100.0/60.0)/60.0;
35
36   # Val is in DGM
37   elif abs(Val) >= 180 or len(Parts[0]) > 3:
38      Val = Val/100.0;
39      Min = Val - long(Val);
40      Val = long(Val) + Min*100.0/60.0;
41      
42   if Val >= 0:
43      return "+" + str(Val);
44   return str(Val);
45
46 # Main program starts here
47 User = pwd.getpwuid(posix.getuid())[0];
48 BindUser = User;
49 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
50 for (switch, val) in options:
51    if (switch == '-u'):
52       User = val
53
54 # Connect to the ldap server
55 l = ldap.open(LDAPServer);
56 print "Accessing LDAP directory as '" + User + "'";
57 Password = getpass(User + "'s password: ");
58 UserDn = "uid=" + User + "," + BaseDn;
59 l.simple_bind_s(UserDn,Password);
60
61 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
62          ["uid","cn","mn","sn","latitude","longitude"]);
63
64 #ttrs = [('uid=bma,ou=users,dc=debian,dc=org', {'longitude': ['-0771426.059'], 'sn': ['Almeida'], 'cn': ['Brian'], 'latitude': ['0384514.263'], 'uid': ['bma']}), ('uid=jgg,ou=users,dc=debian,dc=org', {'longitude': ['-11328'], 'sn': ['Gunthorpe'], 'cn': ['Jason'], 'latitude': ['+5333'], 'uid': ['jgg']})]
65 Attrs.sort();
66
67 print "Markers file will be written to markers.dat,",
68 sys.stdout.flush();
69 F = open("markers.dat","w");
70 Count = 0;
71 for x in Attrs:
72    if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
73       continue;
74    Count = Count + 1;
75    try:
76       F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude"),GetAttr(x,"uid"),EmailAddress(x)));
77    except:
78       F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
79 F.close();
80 print Count,"entries.";