transform_coord: Transform coordinates from/to WGS-84, GCJ-02 and BD-09...

Description Usage Arguments Value Note Author(s) References See Also Examples

Description

There are three commonly seen coordinate systems:

The functions in this family encrypts coordinates from 'WGS-84' to 'GCJ-02' and 'BD-09', and decrypts them back vice versa. The built-in algorithm called by these functions is open source and comes with absolutely no ganrantee.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
wgs_to_gcj(y, ..., force = FALSE, accurate_cn_bou = TRUE)

gcj_to_wgs(y, ..., force = FALSE, accurate_cn_bou = TRUE)

gcj_to_bd(y, ..., force = FALSE, accurate_cn_bou = TRUE)

bd_to_gcj(y, ..., force = FALSE, accurate_cn_bou = TRUE)

wgs_to_bd(y, ..., force = FALSE, accurate_cn_bou = TRUE)

bd_to_wgs(y, ..., force = FALSE, accurate_cn_bou = TRUE)

Arguments

y

one of

  • coordinate pair: a list of coordinate vectors list(lat1, lon1), in which case you can provide other coordiante vectors in ...

  • latitude vector: latitude number(s), in which case you need to provice longitute number(s) in ...

  • coordinate matrix: a matrix (row 1-2 or col 1-2). The function will choose how to read the data

  • coordinate data.frame: a data.frame (col 1-2)

...

one of

  • coordinate pairs: when y is a list containing a coordinate pair (e.g., list(lat1, lon1)), you can pass other coordinate pairs here (e.g., list(lat2, lon2), list(lat3, lon3), ...).

  • longitude vector: when y is only latitude vector, you can pass x (longitude) here.

  • when y is a coordinate matrix or data.frame, ... is omitted.

force

logical, whether convert the coordinates regardless if they locate inside China. Default FALSE, indicating that only coordinates inside China will be converted.

accurate_cn_bou

logical, whether to convert the coordinates based on accurate China boundary. Only effective when api is NULL (local mode). Default TRUE. The WGS-84 ==> GCJ-02 conversion will only be conducted in coordinates inside China. When using the accurate China boundary, the function will check if the coordinates are actually inside China's territory. When set FALSE, the function will use a very vague rectangle to reprensent China boundary.

Value

A 2-col data.frame ([lng, lat]) of transformed coordinates. Note that points out of China will not have the coordinates converted.

Note

Latitude is the horizontal line serving as y-axis metric, longitude is the vertical line serving as x-axis metric.

Author(s)

Yiying Wang, wangy@aetna.com

References

https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936

See Also

Examples

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
## Not run: 

## ========== wgs_to_gcj ==========

# Tiananmen square's WGS-84 coordinate is c(39.90734, 116.39089)
# http://www.google.cn/maps/place/Tiananmen,+Dongcheng,+Beijing/
# @39.90874,116.39713,16z?hl=en

## Single point
wgs_to_gcj(list(39.90734, 116.39089))  # or
wgs_to_gcj(39.90734, 116.39089)  # get
#           lat      lng
# [1,] 39.90874 116.3971

## Multiple points
### Coordinate pairs or Lat / Lon vectors
wgs_to_gcj(list(39.90734, 116.39089), list(39.90734, 116.39089))  # or
wgs_to_gcj(c(39.90734, 39.90734), c(116.39089, 116.39089))  # get
#           lat      lng
# [1,] 39.90874 116.3971
# [2,] 39.90874 116.3971

### Matrix
m <- matrix(c(39.90734, 116.39089, 39.90734, 116.39089, 39.90734, 
              116.39089), nrow=2)
m
#           [,1]      [,2]      [,3]
# [1,]  39.90734  39.90734  39.90734
# [2,] 116.39089 116.39089 116.39089

wgs_to_gcj(m)  # get
#           lat      lng
# [1,] 39.90874 116.3971
# [2,] 39.90874 116.3971
# [3,] 39.90874 116.3971

### data.frame
df <- data.frame(lat=c(39.90734, 39.90734, 39.90734, NA),
                 lon=c(116.39089, 116.39089, 116.39089, 116.39089))
wgs_to_gcj(df)  # get
#           lat      lng
# [1,] 39.90874 116.3971
# [2,] 39.90874 116.3971
# [3,] 39.90874 116.3971
# [4,]       NA       NA

## End(Not run)

## Not run: 

## ========== gcj_to_wgs ==========

# Tiananmen square's GCJ-02 coordinate is c(39.908746, 116.397131)
# http://www.openstreetmap.org/#map=19/39.90734/116.39089

## Single point
gcj_to_wgs(list(39.908746, 116.397131))  # or
gcj_to_wgs(39.908746, 116.397131)  # get
#           lat       lng
# [1,] 39.90734  116.3909

## Multiple points
### Coordinate pairs or lat / lon vectors
gcj_to_wgs(list(39.908746, 116.397131), list(39.908746, 116.397131))  # or
gcj_to_wgs(c(39.908746, 39.908746), c(116.397131, 116.397131))  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909

### Matrix
m <- matrix(c(39.908746, 116.397131, 39.908746, 116.397131, 39.908746, 
              116.397131), nrow=2)
m
#           [,1]       [,2]       [,3]
# [1,]  39.90875   39.90875   39.90875
# [2,] 116.39713  116.39713  116.39713
gcj_to_wgs(m)  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909
# [3,] 39.90734  116.3909

### data.frame
df <- data.frame(lat=c(39.908746, 39.908746, 39.908746, NA),
                 lon=c(116.397131, 116.397131, 116.397131, 116.397131))
gcj_to_wgs(df)  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909
# [3,] 39.90734  116.3909
# [4,]       NA        NA

## End(Not run)

## Not run: 

## ========== gcj_to_bd ==========

# Tiananmen square's GCJ-02 coordinate is c(39.908746, 116.397131)
#http://api.map.baidu.com/marker?location=39.91509,116.40350&title=Tiananmen&
 content=Tiananmen%20square&output=html

## Single point
gcj_to_bd(list(39.908746, 116.397131))  # or
gcj_to_bd(39.908746, 116.397131)  # get
#           lat       lng
# [1,] 39.91509  116.4035

## Multiple points
### Coordiante pairs or lat / lon vectors
gcj_to_bd(list(39.908746, 116.397131), list(39.908746, 116.397131))  # or
gcj_to_bd(c(39.908746, 39.908746), c(116.397131, 116.397131))  # get
#           lat       lng
# [1,] 39.91509  116.4035
# [2,] 39.91509  116.4035

### Matrix
m <- matrix(c(39.908746, 116.397131, 39.908746, 116.397131, 39.908746, 
              116.397131), nrow=2)
m
#           [,1]       [,2]       [,3]
# [1,]  39.90875   39.90875   39.90875
# [2,] 116.39713  116.39713  116.39713
gcj_to_bd(m)  # get
#           lat       lng
# [1,] 39.91509  116.4035
# [2,] 39.91509  116.4035
# [3,] 39.91509  116.4035

### data.frame
df <- data.frame(lat=c(39.908746, 39.908746, 39.908746, NA),
                 lon=c(116.397131, 116.397131, 116.397131, 116.397131))
gcj_to_bd(df)  # get
#           lat       lng
# [1,] 39.91509  116.4035
# [2,] 39.91509  116.4035
# [3,] 39.91509  116.4035
# [4,]       NA        NA

## End(Not run)

## Not run: 

## ========== bd_to_gcj ==========

# Tiananmen square's BD-06 coordinate is c(39.91509, 116.40350)
# http://www.google.cn/maps/place/Tiananmen,+Dongcheng,+Beijing/
# @39.90875,116.39713,16z?hl=en

## Single point
bd_to_gcj(list(39.91509, 116.40350))  # or
bd_to_gcj(39.91509, 116.40350)  # get
#           lat       lng
# [1,] 39.90875  116.3971

## Multiple points
### coordinate pairs or lat / lon vectors
bd_to_gcj(list(39.91509, 116.40350), list(39.91509, 116.40350))  # or
bd_to_gcj(c(39.91509, 39.91509), c(116.40350, 116.40350))  # get
#           lat       lng
# [1,] 39.90875  116.3971
# [2,] 39.90875  116.3971

### Matrix
m <- matrix(c(39.91509, 116.40350, 39.91509, 116.40350, 39.91509, 
              116.40350), nrow=2)
m
#           [,1]       [,2]       [,3]
# [1,]  39.91509   39.91509   39.91509
# [2,] 116.40350  116.40350  116.40350
bd_to_gcj(m)  # get
#           lat       lng
# [1,] 39.90875  116.3971
# [2,] 39.90875  116.3971
# [3,] 39.90875  116.3971

### data.frame
df <- data.frame(lat=c(39.91509, 39.91509, 39.91509, NA),
                 lon=c(116.40350, 116.40350, 116.40350, 116.40350))
bd_to_gcj(df)  # get
#           lat       lng
# [1,] 39.90875  116.3971
# [2,] 39.90875  116.3971
# [3,] 39.90875  116.3971
# [4,]       NA        NA

## End(Not run)

## Not run: 

## ========== wgs_to_bd ==========

# Tiananmen square's WGS-84 coordinate is c(39.90734, 116.39089)
# http://api.map.baidu.com/marker?location=39.91509,116.40350&title=
# Tiananmen&content=Tiananmen%20square&output=html

## Single point
wgs_to_bd(list(39.90734, 116.39089))  # or
wgs_to_bd(39.90734, 116.39089)  # get
#           lat       lng
# [1,] 39.91508  116.4035

## Multiple points
### Coordinate pairs or lat / lon vectors
wgs_to_bd(list(c(39.90734, 116.39089), c(39.90734, 116.39089)))  # or
wgs_to_bd(c(39.90734, 39.90734), c(116.39089, 116.39089))  # get
#           lat       lng
# [1,] 39.91508  116.4035
# [2,] 39.91508  116.4035

### Matrix
m <- matrix(c(39.90734, 116.39089, 39.90734, 116.39089, 39.90734, 
              116.39089), nrow=2)
m
#           [,1]       [,2]       [,3]
# [1,]  39.90734   39.90734   39.90734
# [2,] 116.39089  116.39089  116.39089
wgs_to_bd(m)  # get
#           lat       lng
# [1,] 39.91508  116.4035
# [2,] 39.91508  116.4035
# [3,] 39.91508  116.4035

### data.frame
df <- data.frame(lat=c(39.90734, 39.90734, 39.90734, NA),
                 lon=c(116.39089, 116.39089, 116.39089, 116.39089))
wgs_to_bd(df)  # get
#           lat       lng
# [1,] 39.91508  116.4035
# [2,] 39.91508  116.4035
# [3,] 39.91508  116.4035
# [4,]       NA        NA

## End(Not run)

## Not run: 

## ========== bd_to_wgs ==========

# Tiananmen square's BD-06 coordinate is c(39.91509, 116.40350)
# http://www.openstreetmap.org/#map=19/39.90734/116.39089

## Single point
bd_to_wgs(list(39.91509, 116.40350))  # or
bd_to_wgs(39.91509, 116.40350)  # get
#           lat       lng
# [1,] 39.90734  116.3909

## Multiple points
### Coordinate pairs or lat / lon vectors
bd_to_wgs(list(39.91509, 116.40350), list(39.91509, 116.40350))  # or
bd_to_wgs(c(39.91509, 39.91509), c(116.40350, 116.40350))  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909

### Matrix
m <- matrix(c(39.91509, 116.40350, 39.91509, 116.40350, 39.91509, 
              116.40350), nrow=2)
m
#           [,1]       [,2]       [,3]
# [1,]  39.90734   39.90734   39.90734
# [2,] 116.39089  116.39089  116.39089
bd_to_wgs(m)  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909
# [3,] 39.90734  116.3909

### data.frame
df <- data.frame(lat=c(39.91509, 39.91509, 39.91509, NA),
                 lon=c(116.40350, 116.40350, 116.40350, 116.40350))
bd_to_wgs(df)  # get
#           lat       lng
# [1,] 39.90734  116.3909
# [2,] 39.90734  116.3909
# [3,] 39.90734  116.3909
# [4,]       NA        NA

## End(Not run)

madlogos/asesgeo documentation built on Aug. 9, 2019, 9:53 a.m.