3 # Generate an xearth database from the LDAP entries
4 # LDAP entires for lat/long can be in one of 3 different formats
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
17 import string, re, time, ldap, getopt, sys, pwd, posix;
18 from userdir_ldap import *;
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));
25 if (abs(Val) >= 1806060.0):
26 raise ValueError,"Too Big";
29 if abs(Val) >= 18060.0 or len(Parts[0]) > 5:
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;
37 elif abs(Val) >= 180 or len(Parts[0]) > 3:
39 Min = Val - long(Val);
40 Val = long(Val) + Min*100.0/60.0;
43 return "+" + str(Val);
46 # Main program starts here
47 User = pwd.getpwuid(posix.getuid())[0];
49 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
50 for (switch, val) in options:
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);
61 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
62 ["uid","cn","mn","sn","latitude","longitude"]);
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']})]
67 print "Markers file will be written to markers.dat,",
69 F = open("markers.dat","w");
72 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
76 F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude"),GetAttr(x,"uid"),EmailAddress(x)));
78 F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
80 print Count,"entries.";