-
Notifications
You must be signed in to change notification settings - Fork 5
/
Search-OSMGeoCoordinates.ps1
115 lines (94 loc) · 5.07 KB
/
Search-OSMGeoCoordinates.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function Search-OSMGeoCoordinates
{
<#
.SYNOPSIS
Performs a Geocoodinate lookup to the Nominatim Search API endpoint
Author: n0perator
.DESCRIPTION
Builds a set of URI parameters that are defined by the API and performs
a query to the "search" endpoing at nominatim.openstreetmap.org.
.PARAMETER Query
Specifies a query to search and is an alternative to the specific parameters listed
below this parameter.
.PARAMETER Street
Specifies the street name. The structure should be <housenumber> <streetname>
.PARAMETER City
Secifies the city to search.
.PARAMETER County
Secifies the county to search.
.PARAMETER State
Secifies the state to search. This parameter can be the full name or two-letter
state abbreviation
.PARAMETER Country
Secifies the state to search. This parameter can be the full name or abbreviation
.PARAMETER Postalcode
Secifies the postal code using the shorthand 5-digit or full code
.PARAMETER Format
JSON is the defualt output if a format is specified. Acceptable formats are listed in the notes section.
.PARAMETER WebRequestParams
Specifies a hashtable of parameters to pass to Invoke-WebRequest. Useful if you need to specify a proxy or
credentials when performing the web request.
.EXAMPLE
Search-OSMGeoCoordinates -query "Orlando, FL"
returns a Powershell object
.EXAMPLE
Search-OSMGeoCoordinates -query "Orlando, FL" -format json
returns a JSon object
.EXAMPLE
Search-OSMGeoCoordinates -City "Orlando" -State "FL" -Country "United States"
.EXAMPLE
$RequestParams = @{ Proxy ='http://<proxy_address>:<port>'; ProxyUseDefaultCredentials = $True}
Search-OSMGeoCoordinates -City "Orlando" -state "FL" -Country "United States" -WebRequestParams $RequestParams
.NOTES
If a query is too specific, it may not return results. Broadening the search by removing certain parameters may
help.
Query format: https://nominatim.openstreetmap.org/search?<params>
Use the Verbose parameter to view resulting URI query string.
Params:
q=<query>
street=<housenumber> <streetname>
city=<city>
county=<county>
state=<state>
country=<country>
postalcode=<postalcode>
street=<housenumber> <streetname>
Supported output formats: xml|json|jsonv2|geojson|geocodejson
.LINK
https://nominatim.org/release-docs/develop/api/Search/
#>
[CmdletBinding(DefaultParameterSetName='Detailed')]Param(
[Parameter(ParameterSetName='Query', Position=0)][Alias("Query")][String]$Q,
[Parameter(ParameterSetName='Detailed', Position=0)][String]$Street,
[Parameter(ParameterSetName='Detailed', Position=1)][String]$City,
[Parameter(ParameterSetName='Detailed', Position=2)][String]$County,
[Parameter(ParameterSetName='Detailed', Position=3)][String]$State,
[Parameter(ParameterSetName='Detailed', Position=4)][String]$Country,
[Parameter(ParameterSetName='Detailed', Position=5)][String]$Postalcode,
[Parameter(ParameterSetName='Detailed', Mandatory=$False, Position=6)]
[Parameter(ParameterSetName='Query', Mandatory=$False, Position=1)]
[ValidateSet("psobject","xml","json","jsonv2","geojson","geocodejson")][String]$Format,
[Parameter(Mandatory=$False)][Hashtable]$WebRequestParams
)
#Parse out Parameter names, excluding built-in
$Caller = Get-Command $($PSCmdlet.MyInvocation.InvocationName)
$Allkeys = [array]$Caller.Parameters.keys
#Any parameter not given a ParameterSetName falls under __AllParameterSets
$ParamKeys = $Allkeys.where({ $Caller.parameters[$_].ParameterSets.keys -ne '__AllParameterSets' })
$Keys = [array]$PSBoundParameters.keys.Where({ $_ -in $ParamKeys })
#Format the parameter keys & values for the URI request
$Params = $keys.ForEach({ "$_=$($PSBoundParameters[$_].replace(' ','+'))" })
#Build the compiled URI search parameters
$UriParams= $($Params -join "&").ToLower()
$SearchUri = 'https://nominatim.openstreetmap.org/search?{0:s}' -f $UriParams
#If format isn't specified, use json for the request, but return a PSObject
if($UriParams -inotmatch 'format')
{
$SearchUri += "&format=json"
return $((Invoke-WebRequest -Uri $SearchUri @WebRequestParams).content | ConvertFrom-Json)
}
else
{
return $(Invoke-WebRequest -Uri $SearchUri @WebRequestParams).content
}
}