Search code examples
javageolocationmapbox

Mapbox API for Geocoding causing out of memory exception when running multiple records


When implementing the Mapbox API into a java program I am getting the following error.

enter image description here

This program works fine when sending it a single or a few records but when I am sending the whole excel spreadsheet of (Approx. 11,000) locations. it will send this error about halfway through. Attached is the code on how I am attempting to Geolocate.

for(int i = 0; i < LocationAddresses.size();i++) {
            GeoCodeObject(LocationAddresses.get(i));    
        }

This method is the method that does the geolocating.

private static void GeoCodeObject(String LocationAddress){

            MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
                    .accessToken(MAPBOX_API_TOKEN)
                    .query(LocationAddress)
                    .build();

            mapboxGeocoding.enqueueCall(new Callback<GeocodingResponse>() {
                @Override
                public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

                    List<CarmenFeature> results = response.body().features();

                    if (results.size() > 0) {
                      Point firstResultPoint = results.get(0).center();
                      geocodedLocations.add(firstResultPoint);
                      return;

                    } else {    
                        UndefinedLocation.add(mapboxGeocoding);
                        return;
                    }
                }

                @Override
                public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
                    throwable.printStackTrace();
                }
            });
    }

(Address locations is just a array list of addresses) Is this an issue that can be resolved via programming or is this a hardware limitation? If it can be solved via a different programming strategy how could it be done? Thank you in advance!


Solution

  • You are almost certainly running into the Geocoding API's rate limit of 600 requests per minute: https://docs.mapbox.com/api/search/#geocoding-restrictions-and-limits

    I would recommend adding a wait mechanism to your GeoCodeObject function to prevent your app from making all the requests against the API as fast as it can.


    ⚠️ Disclaimer: I currently work at Mapbox ⚠️