0
votes

I'm using mapbox geocoder for finding latitude and longitude from zip-codes.Problem is it sometimes work just fine and somtimes doesn't work at all.When it doesn't work,it return index error out of bounds,but in my terminal also shows internal server error. What can be done it this situation?

My code is bellow:

    def get_context_data(self, **kwargs):
        context = super(SingleCenterDetailView, self).get_context_data(**kwargs)
        zip_code = self.object.center.zip_code
        geocoder = Geocoder(access_token=mapbox_access_token)
        response = geocoder.forward(str(zip_code))
        response = response.geojson()['features'][0]
        resp = response.get('center')
        geo = [resp[0],resp[1]]
        context['geo'] = geo
        return context

And the problem is in line response = response.geojson()['features'][0] becouse when it works good,when I print this line it shows dict like this

{'text': '53-334', 'context': [{'text': 'Wrocław', 'wikidata': 'Q1799', 'id': 'place.8365052709251970'}, {'short_code': 'PL-DS', 'text': 'Dolnośląskie', 'wikidata': 'Q54150', 'id': 'region.25860'}, {'short_code': 'pl', 'text': 'Poland', 'wikidata': 'Q36', 'id': 'country.340'}], 'geometry': {'coordinates': [17.026519, 51.096412], 'type': 'Point'}, 'center': [17.026519, 51.096412], 'type': 'Feature', 'relevance': 1, 'bbox': [17.024936, 51.095263, 17.028411, 51.09752], 'place_name': '53-334, Wrocław, Dolnośląskie, Poland', 'place_type': ['postcode'], 'properties': {}, 'id': 'postcode.5334805015388220'}

when it doesn't work it prints nothing (thats this index error)

So my question is,can anything be done to solve this? I mean,if this sometimes work and sometimes does not then its probably mean that for some zip-codes it cannot find latitude and longitude,but maybe is there some workaround? Or maybe problem is much more trivial and I simply did someting wrong? (I can even imagine it is some Django problem beocuse of this internal server error)

1

1 Answers

0
votes

Ok so I have founded solution and it is much simpler than i thought.Sometimes zip code it isn't enough.So instead of zip code,you have to pass to geocoder.forward() entire address,like 'Szaserów Warszawa 04-141` then it should works pretty well.