• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gcivil-nyu-org / team5-wed-fall25 / 71

19 Nov 2025 08:34PM UTC coverage: 92.292% (-1.1%) from 93.343%
71

Pull #124

travis-pro

web-flow
Merge dfdf90346 into d8eff4815
Pull Request #124: resolved bug 118

1317 of 1427 relevant lines covered (92.29%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.59
/map_utils/python/utils.py
1
import requests
1✔
2
from django.conf import settings
1✔
3

4

5
def geocode_address(address):
1✔
6
    """
7
    Convert an address in str format to latitude/longitude coordinates using Mapbox Geocoding API.
8
    Returns:
9
        list or None: [longitude, latitude] if successful, None if geocoding fails
10

11
    """
12
    # Check if Mapbox token is configured
13
    if not settings.MAPBOX_ACCESS_TOKEN:
1✔
14
        print("Warning: MAPBOX_ACCESS_TOKEN not configured")
1✔
15
        return None
1✔
16

17
    # Build the Mapbox Geocoding API URL
18
    # Format: https://api.mapbox.com/geocoding/v5/mapbox.places/{search_text}.json
19
    base_url = "https://api.mapbox.com/geocoding/v5/mapbox.places"
×
20
    encoded_address = requests.utils.quote(address)  # URL-encode the address
×
21

22
    # url -> base_url + encoded_address
23
    url = f"{base_url}/{encoded_address}.json"
×
24

25
    # API parameters
26
    params = {
×
27
        "access_token": settings.MAPBOX_ACCESS_TOKEN,
28
        "limit": 1,  # Only return the best matching result
29
        "types": "address,place",  # Focus on addresses and places
30
    }
31

32
    try:
×
33
        # Make HTTP GET request to Mapbox API
34
        response = requests.get(url, params=params, timeout=5)
×
35
        response.raise_for_status()  # Raise exception for 4xx/5xx status codes
×
36

37
        # Parse JSON response
38
        data = response.json()
×
39

40
        # Check if we got valid results
41
        if data.get("features") and len(data["features"]) > 0:
×
42
            # Extract coordinates from the first (best) result
43
            # Format: features[0].geometry.coordinates = [longitude, latitude]
44
            coordinates = data["features"][0]["geometry"]["coordinates"]
×
45
            return coordinates  # [lng, lat]
×
46

47
        # No results found for this address
48
        print(f"No geocoding results found for address: {address}")
×
49
        return None
×
50

51
    except requests.exceptions.Timeout:
×
52
        print(f"Geocoding timeout for address: {address}")
×
53
        return None
×
54
    except requests.exceptions.RequestException as e:
×
55
        print(f"Geocoding request error: {e}")
×
56
        return None
×
57
    except (KeyError, IndexError, ValueError) as e:
×
58
        print(f"Geocoding response parsing error: {e}")
×
59
        return None
×
60

61

62
def get_static_map_url(longitude, latitude, width=600, height=400, zoom=14):
1✔
63
    """
64
    Generate a static map image URL for a given location.
65

66
    Args:
67
        longitude (float): Longitude coordinate
68
        latitude (float): Latitude coordinate
69
        width (int): Image width in pixels
70
        height (int): Image height in pixels
71
        zoom (int): Zoom level (0-22, where 14 is neighborhood level)
72

73
    Returns:
74
        str: URL to static map image
75

76
    Example:
77
    --------
78
    >>> get_static_map_url(-73.9876, 40.7589)
79
    'https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/...'
80
    """
81
    if not settings.MAPBOX_ACCESS_TOKEN:
×
82
        return None
×
83

84
    # Mapbox Static Images API format
85
    # https://api.mapbox.com/styles/v1/{username}/{style_id}/static/{overlay}/{lon},{lat},{zoom}/{width}x{height}
86

87
    # Add a red marker at the location
88
    marker = f"pin-s+ff0000({longitude},{latitude})"
×
89

90
    url = (
×
91
        f"https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/"
92
        f"{marker}/{longitude},{latitude},{zoom}/{width}x{height}"
93
        f"?access_token={settings.MAPBOX_ACCESS_TOKEN}"
94
    )
95

96
    return url
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc