cec3308a091b276a105f0129d679bec19a8634cb
[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 Anon = 0;
21
22 # This needs to check for leading 0 to disambiguate some things
23 def DecDegree(Attr,Type):
24   Parts = re.match('[+-]?(\d*)\\.?(\d*)?',GetAttr(Attr,Type)).groups();
25   Val = string.atof(GetAttr(Attr,Type));
26
27   if (abs(Val) >= 1806060.0):
28      raise ValueError,"Too Big";
29
30   # Val is in DGMS
31   if abs(Val) >= 18060.0 or len(Parts[0]) > 5:
32      Val = Val/100.0;
33      Secs = Val - long(Val);
34      Val = long(Val)/100.0;
35      Min = Val - long(Val);
36      Val = long(Val) + (Min*100.0 + Secs*100.0/60.0)/60.0;
37
38   # Val is in DGM
39   elif abs(Val) >= 180 or len(Parts[0]) > 3:
40      Val = Val/100.0;
41      Min = Val - long(Val);
42      Val = long(Val) + Min*100.0/60.0;
43      
44   if Anon != 0:
45       Str = "%3.3f"%(Val);
46   else:
47       Str = str(Val);
48   if Val >= 0:
49      return "+" + Str;
50   return Str;
51
52 # Main program starts here
53 User = pwd.getpwuid(posix.getuid())[0];
54 BindUser = User;
55 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
56 for (switch, val) in options:
57    if (switch == '-u'):
58       User = val;
59    if (switch == '-a'):
60       Anon = 1;
61
62 # Connect to the ldap server
63 l = ldap.open(LDAPServer);
64 print "Accessing LDAP directory as '" + User + "'";
65 Password = getpass(User + "'s password: ");
66 UserDn = "uid=" + User + "," + BaseDn;
67 l.simple_bind_s(UserDn,Password);
68
69 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
70          ["uid","cn","mn","sn","latitude","longitude"]);
71
72 Attrs.sort();
73
74 print "Markers file will be written to markers.dat,",
75 sys.stdout.flush();
76 F = open("markers.dat","w");
77 Count = 0;
78 Failed = 0;
79 for x in Attrs:
80    if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
81       continue;
82    Count = Count + 1;
83    try:
84       if Anon != 0:
85          F.write("%8s %8s \"\"\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude")));
86       else:
87          F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude"),GetAttr(x,"uid"),EmailAddress(x)));
88    except:
89       Failed = Failed + 1;
90       if Anon == 0:
91          F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
92       else:
93          F.write("# Failed => %s: %s\n" %(sys.exc_type,sys.exc_value));
94 F.close();
95 print Count,"entries,",Failed,"failures.";