TransWikia.com

How to Handle Negative Coordinates When Converting Long/Lat to Pixel Values

Geographic Information Systems Asked by BassoftheC on November 1, 2021

I’m writing a short script that draws white lines on a PNG file to represent roads. I’m using the coordinates from a .geoJSON file to draw the individual line segments. Unfortunately, because some of the coordinate values are negative, my image is stretched and thus becomes inaccurate compared to the actual data. How should I deal with these values?

Here is the conversion equation I’m using:
x = image_width * ((long-min_long)/(max_long-min_long))
"long" is the longitude coordinate in the geoJSON file
min_long and max_long are the mins and maxes of the input coordinates

Command:
python draw_roads.py C:/Users/'user'/Downloads/roads.geojson --world_bounds -74.016536 40.703922 -74.00874 40.701813 --image_size 2000 2000

Inputs:
FilePath: Location of .geoJSON file
–world_bounds: Real-world coordinates of the bounds of geoJSON file
–image_size: Size of output image

One Answer

You don't need to worry about negative coordinates**.

You need to work out the height & width of each png pixel ((max - min) / height or width) and use that to calculate how many pixels from min lon/lat the coordinate is.

eg.

width, height = 2000, 2000
lon, lat = -74.012638, 40.702867
min_long, max_lat, max_long, min_lat = -74.016536, 40.703922, -74.00874, 40.701813

px = int((lon - min_long) / ((max_long - min_long) / width) + 0.5)  # int + 0.5 to round)
py = int((lat - min_lat) / ((max_lat - min_lat) / height) + 0.5)

print(px,py)
# 1000, 1000

# And broken down a bit more only for lon just to demonstrate what the above is doing
pixels_per_degree = (max_long - min_long) / width
pixels_from_min = (lon - min_long) / pixels_per_degree
pixels_from_min = int(pixels_from_min + 0.5)
print(pixels_from_min)

** Though if you get a negative px or py or either is greater than height, width then your input coordinate is outside your min-max lon or lat bounds.

Answered by user2856 on November 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP