GeoLocation - 解析// Formatted_Address?

问题描述:

我正在玩一个AccessDB应用程序中的新函数来从医院名称返回Lat/Lon信息。以下功能提供我需要的名称,当我提供一个名称&地址。我注意到(意外的)该函数返回一个格式化的地址,即使我只提供一个有效的医院名称。我想我可以利用这个来将地址信息回填到我的数据库中。GeoLocation - 解析// Formatted_Address?

看来Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text是“大部分”一致的,并且易于解析为使用“,”作为分隔符来获取地址/城市/州/邮政编码信息。我的并发症是格式化地址字符串中包含“楼层号”的罕见情况。我的分析例程失败。

我发现这个程序(不是我):

Option Explicit 
Option Compare Database 

'Public Type containing the geocoding of the postal address 
Public Type tGeocodeResult 
    dLatitude As Double 
    dLongitude As Double 
    sRetAddress As String 
    sAccuracy As String 
    sStatus As String 
End Type 

'--------------------------------------------------------------------------------------- 
' Procedure : Geocode with Google Geocoding API v3 
' Version : 1.01 
' DateTime : 03/03/2011 
' Author : Philben 
' Purpose : converting addresses into geographic coordinates 
' Parameter : No mandatory. string format or NULL 
' Reference : http://code.google.com/intl/fr-FR/apis/maps/documentation/geocoding/index.html 
' Remark : Query limit of 2,500 geolocation requests per day 
'   : A good accuracy is different of a good geocoding !!! 
'   : Minimum delay between two queries : >= 200 ms 
'--------------------------------------------------------------------------------------- 
Public Function Geocode(Optional ByVal vAddress As Variant = Null, _ 
         Optional ByVal vTown As Variant = Null, _ 
         Optional ByVal vPostCode As Variant = Null, _ 
         Optional ByVal vRegion As Variant = Null, _ 
         Optional ByVal sCountry As String = "UNITED STATES+") As tGeocodeResult 
    On Error GoTo catch 
    Dim oXmlDoc As Object 
    Dim sUrl As String, sFormatAddress As String 
    If Not IsNull(vAddress) Then vAddress = Replace(vAddress, ",", " ") 
    sFormatAddress = (vAddress + ",") & _ 
        (vTown + ",") & _ 
        (vRegion + ",") & _ 
        (vPostCode + ",") & _ 
        sCountry 
    'To create the URL 
    sUrl = "http://maps.googleapis.com/maps/api/geocode/xml?address=" & sFormatAddress & "&sensor=false" 
    ''XMLDOM to get the XML response 
    Set oXmlDoc = CreateObject("Microsoft.XMLDOM") 
    With oXmlDoc 
     .Async = False 
     If .Load(sUrl) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then 
     'Status code 
     Geocode.sStatus = .selectSingleNode("GeocodeResponse/status").Text 
     'If a result is returned 
     If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then 
      'formatted_address 
      Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text 
      'Accuracy 
      Geocode.sAccuracy = .selectSingleNode("//location_type").Text 
      'Latitude and longitude 
      Geocode.dLatitude = Val(.selectSingleNode("//location/lat").Text) 
      Geocode.dLongitude = Val(.selectSingleNode("//location/lng").Text) 
     End If 
     End If 
    End With 
    Set oXmlDoc = Nothing 
    Exit Function 
catch: 
    Set oXmlDoc = Nothing 
    Err.Raise Err.Number, , Err.Description 
End Function 

结果示例(Geocode.sRetAddress - 格式的地址):

好:为100S雷蒙德大道,阿罕布拉,CA 91801,USA
好:3040盐河LN,阿灵顿高地,IL 60005,USA
不好:4楼,2450阿什比大道,伯克利,CA 94705,USA

问题

任何线索,如果格式化地址的“楼层”组件可以排除,或者显式返回JUST所需的组件?

感谢,

马克·佩尔蒂埃

PS>我目前计数的数量“”串并有条件地处理解析任务。但作为一种通用的方法,可能还会有其他例外,我还没有遇到过。

+0

计算逗号的数量(或者说在逗号分割,然后连接)似乎是一个合理的事情。如果您无法证明无法运作的情况,则很难为我们解释它。 –

+0

您可以提供有效的链接吗? – SIM

+0

Shahin,这里是我找到的链接:https://access-programmers.co.uk/forums/showthread.php?t=206247 –

我有点在XPath初学者,但我想我能解决这个问题:

相反的:

'formatted_address 
    Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text 

用途:

'Build an address: 
Geocode.sRetAddress = oXMLDoc.selectSingleNode("descendant::address_component[type='street_number']/short_name").text 
Geocode.sRetAddress = Geocode.sRetAddress & " " oXMLDoc.selectSingleNode("descendant::address_component[type='route']/short_name").text 
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='locality']/short_name").text 
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='administrative_area_level_1']/short_name").text 
Geocode.sRetAddress = Geocode.sRetAddress & " " oXMLDoc.selectSingleNode("descendant::address_component[type='postal_code']/short_name").text 
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='country']/short_name").text 

手动根据Google地图地理编码API提供的组件构建地址。

请注意,如果您正在解析诸如城市和州之类的事情,那么这是一件相当愚蠢的事情,因为它们只在XML文档中可用。你最好直接从XML中读取它们。

只是重新阅读,它看起来像你的具体情况只适用于医院,所以你不需要考虑这里列出的所有问题。不过,如果其他人正在寻求解析不仅仅包含“地板”的地址,我会放弃这一点。还有 - 你可以考虑只找到“根”的算法。

我在一个类似的项目中工作,我需要确定“根”的物理地址,它可能比满足眼睛要复杂得多。有很多缺陷值得注意。我最终不得不建立一个全面的规则引擎。预测每种可能的组合并对其进行解释。

-2 MAIN ST四楼 -2 MAIN ST三楼 -2 MAIN ST单元3 -4th地板2 MAIN ST -Apt 3 2主街道 -Apt 3 22 7号线路 -2 MAIN ST 1st floor ... more ...更多

作为一般规则,您通常会尝试识别格式为“2 Main Street”的地址部分,您有一个号码,一个街道名称和描述该街道的后缀/道路/驾驶等。这是一个通用算法,它只是基础。你需要扩大。

如果有任何逗号,分割字符串成单独的元素被单独评估

在地址元素删除所有标点的“街”的

查找索引你必须有一个相当广泛的列表,但这里有一些:

Road,Rd,St,Boulevard,Blvd,Blv,Way,Avenue,Ave,Kill,Dri​​ve,Dr,Lane,Ln,Path,Highway,Hwy,BiWay, Bwy,高速公路。 Circle,Cir,穿越,邢,路线,Rte,农村路线,RR 我相信你能想到更多。

找到其中一个的最右侧实例,并从该索引向后工作,直到找到数值(或更准确地说,连续的一组数值的开始索引)。 - 确保数字值不是街道名称的一部分(即“第3街道”),这意味着确保数值没有跟随“rd”或“th”或“nd”或确保其具有如果是这样,继续往回看,直到找到街道地址的数字部分 - 一旦找到数字值,您可能会得到所需的数据,并抓取数字值和“街道”之间的所有内容。

其他的事情要小心: - 为“街”和“圣”是相同的缩写,如“2圣弗朗西斯圣” - 缩写为“医生”和“驱动器”是相同的。 。“3 Dr Jones博士” - “路线”和“高速公路”可以具有跟在“2路线5”中的数字值 - “街道/车道”许多化身的缩写经常被埋在街道名称中。 “3 Caveman Arrival St”包含“ave”和“rr”和“st” - 数字部分也可以写成“Three Main Street”中的单词,

如果您选择尝试识别不需要的部分的地址,而不是所需的部分,您同样需要考虑过多的潜在情况:

公寓,公寓,套房,Ste,楼层,楼层,单位,#,平,框,邮政信箱,采购,, Bldg,Bld,Dorm,Room,Rm

最终,您可能会得到很多需要考虑的情况/例外以及许多“案例”。您也可以考虑使用正则表达式来识别它们。祝你好运!