walkerke / mapboxapi

R interface to Mapbox web services
https://walker-data.com/mapboxapi/
Other
110 stars 8 forks source link

mb_matrix() bug #34

Closed bresek closed 1 year ago

bresek commented 2 years ago

When I use the function mb_matrix() it returns the error:

Error: lexical error: invalid char in json text. <!DOCTYPE HTML PUBLIC "-//W3C// (right here) ------^

I specified census block groups as the origins and points as the destinations. I've tried changing the inputs but I always get this result. The other functions work well for me, such as mb_isochrone.

Screenshot 2022-10-25 093141

bresek commented 2 years ago

The full traceback is

Error: lexical error: invalid char in json text.
<!DOCTYPE HTML PUBLIC "-//W3C//
(right here) ------^
6.parse_string(txt, bigint_as_char)
5.parseJSON(txt, bigint_as_char)
4.parse_and_simplify(txt = txt, simplifyVector = simplifyVector, 
simplifyDataFrame = simplifyDataFrame, simplifyMatrix = simplifyMatrix, 
flatten = flatten, ...)
3.jsonlite::fromJSON(., flatten = TRUE)
2.httr::content(request, as = "text") %>% jsonlite::fromJSON(flatten = TRUE)
1.mb_matrix(vb_block_groups, chargers)
elipousson commented 2 years ago

Could you share a reproducible example? The reprex package is helpful for that if you're not already familiar. It looks like there may be an issue with your input origins or destination data but, if that is the case, it would be great to improve the error messages so it is easier to figure out what went wrong.

bresek commented 2 years ago

Oh yes, my bad. In my case, I was trying to use some block groups for Virginia Beach with some points.

library(tidycensus)
#> Warning: package 'tidycensus' was built under R version 4.1.3
library(tigris)
#> Warning: package 'tigris' was built under R version 4.1.3
#> To enable caching of data, set `options(tigris_use_cache = TRUE)`
#> in your R script or .Rprofile.
library(mapboxapi)
#> Warning: package 'mapboxapi' was built under R version 4.1.3
#> Usage of the Mapbox APIs is governed by the Mapbox Terms of Service.
#> Please visit https://www.mapbox.com/legal/tos/ for more information.
dc_block_groups <- block_groups("DC")
#> Retrieving data for the year 2020
#>   |                                                                              |                                                                      |   0%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   5%  |                                                                              |======                                                                |   9%  |                                                                              |=========                                                             |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |=============                                                         |  18%  |                                                                              |===============                                                       |  21%  |                                                                              |================                                                      |  23%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  32%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  36%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |===============================                                       |  45%  |                                                                              |===================================                                   |  49%  |                                                                              |=====================================                                 |  52%  |                                                                              |======================================                                |  54%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  58%  |                                                                              |============================================                          |  63%  |                                                                              |===============================================                       |  67%  |                                                                              |==================================================                    |  72%  |                                                                              |=====================================================                 |  76%  |                                                                              |========================================================              |  81%  |                                                                              |============================================================          |  85%  |                                                                              |===============================================================       |  90%  |                                                                              |==================================================================    |  94%  |                                                                              |======================================================================| 100%
dc_landmarks <- landmarks("DC", type = "point")
#> Retrieving data for the year 2020
#>   |                                                                              |                                                                      |   0%  |                                                                              |====                                                                  |   6%  |                                                                              |======================================================================| 100%
example <- mb_matrix(dc_block_groups, dc_landmarks)
#> Splitting your matrix request into smaller chunks and re-assembling the result.
#> Using feature centroids for origins
#> No encoding supplied: defaulting to UTF-8.
#> Error: lexical error: invalid char in json text.
#>                                        <!DOCTYPE HTML PUBLIC "-//W3C//
#>                      (right here) ------^
walkerke commented 2 years ago

I can reproduce this error. The problem is that we don't support matrices that large internally in mapboxapi. Mapbox has a 25x25 limit for its API; we circumvent that when origins or destinations exceed 25, but I don't have logic to accommodate both origins and destinations exceeding the limit at this time.

The error message here is supposed to be thrown when that type of request is made: https://github.com/walkerke/mapboxapi/blob/master/R/navigation.R#L202-L204; I need to look into why that's not happening.

bresek commented 2 years ago

I see! Thanks for the response- I'm a big fan of your various census-related packages!

walkerke commented 1 year ago

This is now fixed! To get it to work you will need to specify allow_large_matrix = TRUE; this matrix is large enough that you will incur charges to your Mapbox account if you try it, though.