Advertisement
Guest User

AccessToKML

a guest
May 21st, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.44 KB | None | 0 0
  1. Option Compare Database
  2.  
  3.  
  4. Public Sub generateKML()
  5. '
  6. ' GenerateKML Macro
  7. ' Macro recorded 26/09/2006 by simon_a
  8. ' Adapted and imported to Access by SAA
  9. ' 03 aug 2007 - v3.0 - 2007 08 06 19 24
  10. '
  11.  
  12.     ' DECLARE VARIABLES
  13.     Dim filename As String
  14.     Dim docname As String
  15.    
  16.     Dim altitude As String
  17.     Dim range As String
  18.     Dim tilt As String
  19.     Dim heading As String
  20.     Dim description As String
  21.     Dim visibility As Boolean
  22.    
  23.     Dim grouping As Boolean
  24.     Dim grpfield As String
  25.     Dim grpfilter As String
  26.    
  27.     Dim cfieldName As String
  28.     Dim cfieldLat As String
  29.     Dim cfieldLong As String
  30.     Dim cfieldAlt As String
  31.     Dim cfieldDesc As String
  32.     Dim cfieldCoun As String
  33.     Dim cfieldRange As String
  34.     Dim cfieldTilt As String
  35.    
  36.     Dim identa As Integer
  37.     identa = 0
  38.    
  39.     ' GROUPING CONFIGURATION
  40.     ' CREATE A SEPARTE SUBFOLDER TO EACH GROUP
  41.     grouping = True ' GROUPING TROU OR FALSE
  42.     grpfield = "Group" ' FIELD NAME TO BE GROUPED ON
  43.     difffiles = False     ' DIFERENT FILES TO EACH GROUP
  44.     visibility = False    ' AUTOMATIC SHOWING OR NOT
  45.  
  46.     ' GENERAL CONFIGURATION
  47.     filePath = "C:\Users\james\Desktop\In Development" ' SAME PATH AS THE MDB
  48.     filename = "QueryOutput" ' OUTPUT FILE NAME
  49.     docname = "MapDB Query Results" ' KML TITLE AND FOLDER NAME
  50.     databasename = CurrentDb!Queries!qryDataExport ' SOURCE TABLE OR QUERY
  51.    
  52.     ' RESPECTIVE COLLUM NAMES RELATIVE TO EACH FILTER
  53.     ' REMEMBER THAT LAT AND LONG MUST BE IN DEC OF DEGREE
  54.     ' AND NOT IN MINUTES
  55.     cfieldName = "Map_Number" ' NAME OF THE SITE
  56.     cfieldLat = "latitude"        ' LATITUDE
  57.     cfieldLong = "longitude"      ' LONGITUDE
  58.     cfieldAlt = ""           ' ALTITUDE
  59.     cfieldDesc = "Map_Type" ' DESCRIPTION
  60.     cfieldCoun = "country"   ' COUNTRY
  61.     cfieldRange = ""         ' RANGE
  62.     cfieldTilt = ""          ' TILT
  63.    
  64.     ' VALUES IF NOT DEFINED IN THE TABLE
  65.     ' IF FIND IN THE TABLE THE DEFAULT VALUE
  66.     ' WILL BE ERASED
  67.     altitude = "0"
  68.     range = "68424.19526792552"
  69.     tilt = "2.022197391423853e-010"
  70.     heading = "-0.02880169675294712"
  71.    
  72.     ' OPEN DATABASE
  73.     Dim outputtext As Collection
  74.     Set outputtext = New Collection
  75.    
  76.     ' OPEN DATABASE
  77.     Dim rs As DAO.Recordset
  78.    
  79.     ' GROUPING
  80.     If grouping Then
  81.         ' CREATES A KEY LIST
  82.         Dim keys As DAO.Recordset
  83.         groupcmd = "SELECT [" & databasename & "].[" & grpfield & "] FROM [" & databasename & "] GROUP BY [" & databasename & "].[" & grpfield & "]"
  84.         identa = 1
  85.         Set keys = CurrentDb.OpenRecordset(groupcmd, dbOpenSnapshot)
  86.        
  87.         If Not (difffiles) Then
  88.             ' OPEN FILE
  89.             Close #1
  90.             file = filePath & "\" & filename & ".kml"
  91.             Open file For Output As #1
  92.        
  93.             ' WRITING KML HEADER
  94.             Set outputtext = kmlheader(filename, docname, visibility)
  95.         End If
  96.        
  97.         If Not (keys.BOF And keys.EOF) Then ' There is data
  98.             keys.MoveFirst
  99.             Do Until keys.EOF = True
  100.                 grpfilter = keys.Fields(0).Value
  101.                 'IS DEFINED TO SEPARATE IN DIFFERENT FOLDERS, CREATE A FOLDER LIST
  102.                 If (difffiles) Then
  103.                     ' OPEN FILE
  104.                     Close #1
  105.                     Dim tmpfilename As String
  106.                     Dim tmpdocname As String
  107.                    
  108.                     tmpfilename = filename & "_" & grpfilter & ".kml"
  109.                     tmpdocname = docname & "_" & grpfilter
  110.                     file = filePath & "\" & tmpfilename
  111.                     Open file For Output As #1
  112.                    
  113.                     ' WRITING KML HEADER
  114.                     Set outputtext = kmlheader(tmpfilename, tmpdocname, visibility)
  115.                 End If
  116.  
  117.                 ident1 = ident(identa + 1)
  118.                 ident2 = ident(identa + 2)
  119.                
  120.                 outputtext.Add Item:=ident1 & "<Folder>"
  121.                 outputtext.Add Item:=ident2 & "<name>" & grpfilter & "</name>"
  122.                 outputtext.Add Item:=ident2 & "<open>0</open>"
  123.                 If visibility Then strvisible = "1" Else strvisible = "0"
  124.                 outputtext.Add Item:=ident2 & "<visibility>" & strvisible & "</visibility>"
  125.                
  126.                 Set outputtext = printerpart(outputtext)
  127.                
  128.                 record2open = "SELECT * FROM " & databasename & " WHERE [" & grpfield & "] = """ & grpfilter & """"
  129.                 Set rs = CurrentDb.OpenRecordset(record2open, dbOpenSnapshot)
  130.                 Set outputtext = gatherData(rs, cfieldName, cfieldLat, cfieldLong, cfieldAlt, cfieldDesc, cfieldCoun, cfieldRange, cfieldTilt, altitude, range, tilt, heading, description, identa)
  131.                
  132.                 outputtext.Add Item:=ident1 & "</Folder>"
  133.                 Set outputtext = printerpart(outputtext)
  134.                
  135.                 keys.MoveNext
  136.                 rs.Close
  137.             Loop
  138.         End If
  139.         keys.Close
  140.     Else
  141.         Set rs = CurrentDb.OpenRecordset(databasename)
  142.         identa = 0
  143.        
  144.         ' OPEN FILE
  145.         Close #1
  146.         Open filePath & "\" & filename & ".kml" For Output As #1
  147.        
  148.         ' WRITING KML HEADER
  149.         Set outputtext = kmlheader(filename, docname, visibility)
  150.        
  151.         ' GATHERING DATA AND PRITING PLACEMARK WITHOUT FILTER
  152.         Set outputtext = gatherData(rs, cfieldName, cfieldLat, cfieldLong, cfieldAlt, cfieldDesc, cfieldCoun, cfieldRange, cfieldTilt, altitude, range, tilt, heading, description, identa)
  153.         rs.Close
  154.     End If
  155.    
  156.     ' WRITING FOOTER OF KML
  157.     Set outputtext = footer()
  158.     Close #1
  159. End Sub
  160.  
  161. Function ident(identa As Integer) As String
  162.     Dim identation As String
  163.     identation = String(identa, vbTab)
  164.     ident = identation
  165. End Function
  166.  
  167. Function printerpart(outputtext As Collection) As Collection
  168.     TotalRecords = outputtext.Count
  169.     For i = 1 To TotalRecords
  170.         outputext = outputtext(i)
  171.         outputext = Replace(outputext, "&", "and")
  172.         Print #1, outputext
  173.     Next i
  174.     Set printerpart = New Collection
  175. End Function
  176.  
  177.  
  178. Function gatherData(rs As Recordset, cfieldName As String, cfieldLat As String, cfieldLong As String, cfieldAlt, cfieldDesc As String, cfieldCoun As String, cfieldRange As String, cfieldTilt As String, altitude As String, range As String, tilt As String, heading As String, description As String, identa As Integer) As Collection
  179.     Dim outputtext As Collection
  180.     Set outputtext = New Collection
  181.     Dim locationname As String
  182.     Dim Longitude As String
  183.     Dim Latitude As String
  184.    
  185.     ' GATHERING THE ACTUAL DATA
  186.     If Not (rs.BOF And rs.EOF) Then ' There is data
  187.         rs.MoveFirst
  188.         Do Until rs.EOF = True
  189.             For i = 0 To rs.Fields.Count - 1
  190.                 If (rs.Fields(i).Name = cfieldName) Then locationname = rs.Fields(i).Value
  191.                 ElseIf (rs.Fields(i).Name = cfieldLat) Then Latitude = rs.Fields(i).Value
  192.                 ElseIf (rs.Fields(i).Name = cfieldLong) Then Longitude = rs.Fields(i).Value
  193.                 ElseIf (rs.Fields(i).Name = cfieldAlt) Then altitude = rs.Fields(i).Value
  194.                 ElseIf (rs.Fields(i).Name = cfieldDesc) Then description = rs.Fields(i).Value
  195.                 ElseIf (rs.Fields(i).Name = cfieldCoun) Then country = rs.Fields(i).Value
  196.                 ElseIf (rs.Fields(i).Name = cfieldRange) Then range = rs.Fields(i).Value
  197.                 ElseIf (rs.Fields(i).Name = cfieldTilt) Then tilt = rs.Fields(i).Value
  198.                 ElseIf (rs.Fields(i).Name = cfieldhead) Then heading = rs.Fields(i).Value
  199.             Next i
  200.        
  201.         ' WRITING THE PLACEMARK PART OF THE KML
  202.         Set outputtext = placemark(locationname, Longitude, Latitude, altitude, range, tilt, heading, description, identa)
  203.         rs.MoveNext
  204.         Loop
  205.     End If
  206.    
  207.     Set gatherData = printerpart(outputtext)
  208. End Function
  209.  
  210. Function footer() As Collection
  211.     Dim outputtext As Collection
  212.     Set outputtext = New Collection
  213.     identa = 0
  214.    
  215.     outputtext.Add Item:=ident(identa + 1) & "</Folder>"
  216.     outputtext.Add Item:="</Document>"
  217.     outputtext.Add Item:="</kml>"
  218.     Set footer = printerpart(outputtext)
  219. End Function
  220.  
  221. Function placemark(locationname As String, Longitude As String, Latitude As String, altitude As String, range As String, tilt As String, heading As String, description As String, identa As Integer) As Collection
  222.     Dim outputtext As Collection
  223.     Set outputtext = New Collection
  224.     ' WRITE PLACEMARK TO EACH SITE
  225.    
  226.     ' IDENTATION
  227.     ident2 = ident(identa + 2)
  228.     ident3 = ident(identa + 3)
  229.     ident4 = ident(identa + 4)
  230.    
  231.     outputtext.Add Item:=ident2 & "<Placemark>"
  232.     outputtext.Add Item:=ident3 & "<name>" & locationname & "</name>"
  233.     outputtext.Add Item:=ident3 & "<LookAt>"
  234.     outputtext.Add Item:=ident4 & "<longitude>" & Longitude & "</longitude>"
  235.     outputtext.Add Item:=ident4 & "<latitude>" & Latitude & "</latitude>"
  236.     outputtext.Add Item:=ident4 & "<altitude>" & altitude & "</altitude>"
  237.     outputtext.Add Item:=ident4 & "<range>" & range & "</range>"
  238.     outputtext.Add Item:=ident4 & "<tilt>" & tilt & "</tilt>"
  239.     outputtext.Add Item:=ident4 & "<heading>" & heading & "</heading>"
  240.     outputtext.Add Item:=ident4 & "<altitudeMode>relativeToGround</altitudeMode>"
  241.     outputtext.Add Item:=ident3 & "</LookAt>"
  242.     outputtext.Add Item:=ident3 & "<styleUrl>#msn_pin</styleUrl>"
  243.     outputtext.Add Item:=ident3 & "<Point>"
  244.     outputtext.Add Item:=ident4 & "<coordinates>" & Longitude & "," & Latitude & ",0</coordinates>"
  245.     outputtext.Add Item:=ident3 & "</Point>"
  246.     outputtext.Add Item:=ident3 & "<description><![CDATA[" & description & "]]></description>"
  247.     outputtext.Add Item:=ident2 & "</Placemark>"
  248.        
  249.     Set placemark = printerpart(outputtext)
  250. End Function
  251.  
  252. Function kmlheader(filename As String, docname As String, visibility As Boolean) As Collection
  253.     Dim outputtext As Collection
  254.     Set outputtext = New Collection
  255.     identa = 0
  256.     ' WRITING KML HEADER
  257.    
  258.     ' INDENTATION
  259.     ident1 = ident(identa + 1)
  260.     ident2 = ident(identa + 2)
  261.     ident3 = ident(identa + 3)
  262.     ident4 = ident(identa + 4)
  263.    
  264.     ' TEXT ITSELF
  265.     outputtext.Add "<?xml version=""1.0"" encoding=""UTF-8""?>"
  266.     outputtext.Add Item:="<kml xmlns=""http://earth.google.com/kml/2.0"">"
  267.     outputtext.Add Item:="<Document>"
  268.    
  269.     outputtext.Add Item:=ident1 & "<name>" & filename & "</name>"
  270.  
  271.     outputtext.Add Item:=ident1 & "<Style id=""sn_pin"">"
  272.     outputtext.Add Item:=ident2 & "<IconStyle>"
  273.     outputtext.Add Item:=ident3 & "<scale>1.1</scale>"
  274.     outputtext.Add Item:=ident3 & "<Icon>"
  275.     outputtext.Add Item:=ident4 & "<href>http://maps.google.com/mapfiles/kml/pal2/icon13.png</href>"
  276.     outputtext.Add Item:=ident3 & "</Icon>"
  277.     outputtext.Add Item:=ident3 & "<hotSpot x=""20"" y=""2"" xunits=""pixels"" yunits=""pixels""/>"
  278.     outputtext.Add Item:=ident2 & "</IconStyle>"
  279.     outputtext.Add Item:=ident1 & "</Style>"
  280.    
  281.     outputtext.Add Item:=ident1 & "<Style id=""sh_pin"">"
  282.     outputtext.Add Item:=ident2 & "<IconStyle>"
  283.     outputtext.Add Item:=ident3 & "<scale>1.5</scale>"
  284.     outputtext.Add Item:=ident3 & "<Icon>"
  285.     outputtext.Add Item:=ident4 & "<href>http://maps.google.com/mapfiles/kml/pal2/icon13.png</href>"
  286.     outputtext.Add Item:=ident3 & "</Icon>"
  287.     outputtext.Add Item:=ident3 & "<hotSpot x=""20"" y=""2"" xunits=""pixels"" yunits=""pixels""/>"
  288.     outputtext.Add Item:=ident2 & "</IconStyle>"
  289.     outputtext.Add Item:=ident1 & "</Style>"
  290.  
  291.     outputtext.Add Item:=ident1 & "<StyleMap id=""msn_pin"">"
  292.     outputtext.Add Item:=ident2 & "<Pair>"
  293.     outputtext.Add Item:=ident3 & "<key>normal</key>"
  294.     outputtext.Add Item:=ident3 & "<styleUrl>#sn_pin</styleUrl>"
  295.     outputtext.Add Item:=ident2 & "</Pair>"
  296.     outputtext.Add Item:=ident2 & "<Pair>"
  297.     outputtext.Add Item:=ident3 & "<key>highlight</key>"
  298.     outputtext.Add Item:=ident3 & "<styleUrl>#sh_pin</styleUrl>"
  299.     outputtext.Add Item:=ident2 & "</Pair>"
  300.     outputtext.Add Item:=ident1 & "</StyleMap>"
  301.  
  302.     outputtext.Add Item:=ident1 & "<Folder>"
  303.     outputtext.Add Item:=ident2 & "<name>" & docname & "</name>"
  304.    
  305.     outputtext.Add Item:=ident2 & "<open>0</open>"
  306.    
  307.     If visibility Then strvisible = "1" Else strvisible = "0"
  308.     outputtext.Add Item:=ident2 & "<visibility>" & strvisible & "</visibility>"
  309.    
  310.     Set kmlheader = printerpart(outputtext)
  311. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement