2

I'm trying to get GeoPandas to give me the distance between suburbs (polygons) and the city centre (a point). However, no matter what coordinate reference system I use for the distance measurement (DIST_CRS), I keep getting the wrong results.

What am I doing wrong? I manually verified using espg.io that the coordinates were being converted correctly, so I don't understand why GeoPandas isn't outputting the correct distance.

This is just a visualization project; I'm not a GIS expert. I don't understand why GeoPandas doesn't offer a simpler way for calculating the great circle distance (given some intermediary CRS). It seems simpler to use the haversine function, but I'd like to use the proper method.

import geopandas

def read_file(filepath):
    with open(filepath, encoding="utf-8") as fh:
        return fh.read()

def add_dist_to_city(suburbs):
    suburbs = suburbs.to_crs(DIST_CRS)
    city_center = geopandas.GeoDataFrame(geometry=geopandas.points_from_xy([MELBOURNE[0]], [MELBOURNE[1]]), crs=GOOGLE_MAPS_CRS).to_crs(DIST_CRS)
    return suburbs["geometry"].apply(lambda suburb: suburb.distance(city_center).mean())

SUBURBS_COLUMN = "vic_loca_2"
GEOMETRY_COLUMN = "geometry"
GOOGLE_MAPS_CRS = "EPSG:3857" # Since we took cordinates from google maps
DIST_CRS = "EPSG:32755" # We're use UTM zone 55
MELBOURNE = [144.96255859257582, -37.813600753509064] # Use x, y

suburbs_raw = geopandas.read_file(read_file("data/suburb-2-vic.geojson"))

# Check what CRS the json use
print(suburbs_raw.crs) # Geographic 2D CRS: EPSG:4326

# Extract suburb name and geometry (note geopandas expect a "geometry" column)
suburbs = suburbs_raw[[SUBURBS_COLUMN, GEOMETRY_COLUMN]].rename(columns={SUBURBS_COLUMN: "suburb", GEOMETRY_COLUMN: "geometry"})

# Capitalize suburb names
suburbs["suburb"] = suburbs["suburb"].map(str.title)

# Calculate distance to city
suburbs["distanceFromCity"] = add_dist_to_city(suburbs)

# Print results
test = suburbs.sort_values(by=["distanceFromCity"])
# Outputs ~16,000 instead of ~234 km
test[test["suburb"]=="Nurran"]["distanceFromCity"].values[0]/1000

In the last line, I'm getting a result of ~16,000 km instead of the actual distance, which is around 234 km. The distances appear to be scaled incorrectly.

I'm using EPSG:32755 to calculate the distances.

Link to datafile: https://github.com/tonywr71/GeoJson-Data/blob/master/suburb-2-vic.geojson

4
  • Welcome to GIS SE. As a new user, please take the Tour. Please Edit the Question to state both the "wrong" value, and the one you believe to be correct (and why).
    – Vince
    Commented May 4 at 12:46
  • 1
    @Vince Thanks, I've edited my question to elaborate a bit more on the incorrect values.
    – aiwl
    Commented May 4 at 12:55
  • 3
    Your X, Y coordinates are not EPSG:3857 they are EPSG:4326. Google Maps displays the map in EPSG:3857 but displays coordinates in latitude, longitude EPSG:4326.
    – user2856
    Commented May 4 at 13:03
  • @user2856 That solved it! I've completely overlooked that. I'm happy to accept your comment as an answer if you repost it.
    – aiwl
    Commented May 4 at 14:20

1 Answer 1

4

Your X, Y coordinates are not EPSG:3857. Google Maps displays the map in EPSG:3857 but displays coordinates in latitude, longitude EPSG:4326

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.