Converting latitude-longitude into x-y cordinates for images

Tuesday, January 22, 2008, 2:33 AM
Source Code by John (Article #171)

UPDATE: You can find a full working piece of code for this project in this later post.

Right now I'm tinkering with building a map system out of the ARC Shapefile datasets available on the internet. These are the files that the Census and the USGS and such use to actually map pretty much everything in the United States.

Not surprisingly, these maps present a challenge. Foremost is shear size and processing power. For the sake of my early experiments, I'm limiting my efforts to local maps (Jefferson County, Pennsylvania) and converting those maps into three things: database entries for dynamic mapping, vector graphics for re-use and raster graphics for web use and for a long-standing project I have to rebuild Google Maps from scratch.

Advertisements


Assembling all this data presents another challenge. Converting the map data into images and vector files. Particularly a bitch is converting latitude and longitude, which have an origin that is to the lower right of the images I intend to generate into typical graphics which have an origin to the upper left of the images that will be generated.

There are times when I regret not having formal training in computer programming. Most of those times are when I wish someone had sat me down and given me a schooling in algorithms, a class that most colleges offer.

Searching the internet yielded no answers of any value, so I eventually sat down and pounded my way through the problem myself.

The model for converting lat-long to x-y requires a few pieces of data.

First, you have to figure out what latitudes and longitudes represent the furthest extents of what you are drawing. This might sound easy, but when you're pushing thousands of pieces of data out of a database it isn't. Also, you have to bear some consideration for margins, scale and just whether your image will have enough detail.

The easiest way to handle this is to store the highest and lowest lats and longs you've encountered in the dataset you are using. If margins matters, you may also want to fudge just a tiny bit more to ensure you have some space past the maximum extents of the data your are drawing.

Second, you have to establish the actual size of the area you just represented. This requires a particular formula for converting latitude and longitude into miles.

Check out the formula:

$distance=3963.0 * acos(sin($lat1/57.2958) * sin($lat2/57.2958) + cos($lat1/57.2958) * cos($lat2/57.2958) * cos($lon2/57.2958 - $lon1/57.2958));

This formula calculates the distance between any two sets of lat-long co-ordinates.

So, for example, the width of your image is represented with the distance from these two co-ordinates:

$lat1=$maxlat;

$long1=$maxlong;

$lat2=$maxlat;

$long2=$minlong;


Similarly, the height of your image is:

$lat1=$maxlat;
$long1=$maxlong;
$lat2=$minlat;
$long2=$maxlong;

Plug those values into the equation and you will derive the height and width of your image. You can multiply them all by the same co-efficient if you wish to expand the size of the image. A co-efficient of 1 yields a scale where one pixel equals one mile. A co-efficient of 2 yields a scale where two pixels equal one mile.

The third thing you need to do is establish the relationship of the points you are drawing relative to the corners of the image. This means that you have to calculate the distance of the latitude from the 0,0 origin (northwest corner of the map) and then separately calculate the distance of the longitude from that corner.

The nice thing here is the pixel co-ordinates are always 0,0. So, everything is in relationship to zero.

So...

$lat1=$maxlat;

$long1=$minlong;

$lat2=$maxlat;

$long2=$coordlong;

This will yield the distance in pixels to the right of your lat-long on the map, relative to the upperleft corner of the map. Like this, as a quick visual...

____________
|-------X
|
|
|
|



And...

$lat1=$maxlat;


$long1=$minlong;


$lat2=$coordlat;


$long2=$minlong;

This give you how many pixels down your point is. Like this...
____________

|!

|!

|!

|x

|


Now you can combine the x and the y co-ordinates you've calculated and place them as a faithful representation onto your image, like so...

Advertisements


____________

| !

| !

| !

|-------X

|



You've just flipped latitude and longitude's bottom/right-handed origin data from every geospatial database in the world into uppler/left-hand origin data typical of every graphics image format in the world.

If you need to draw lines, you just find more co-ordinates and start connecting them to last one. If you pull the CFCC data from the Shapefile data, you can easily color
code the lines to display highways, streams, bridges, etc. all
differently. Pretty soon, your code will pound out maps left and right with little to no human intervention.

It's a small trick, but it is a very handy algorithm for automating what is otherwise a gigantic and vicious task.

I plan to combine this functionality with the database to allow dynamically generated maps of the local area. If a user wants to see all the unpaved roads in the county, all he has to do is check a box and click submit. The database will find all the roads with the CFCC datatype and render only those to the generated map.

Should be fun.



Mail article to a friend

© 2012 Pro Content and Design. All rights reserved.


Tools

Check Google PageRank


Recent articles

  1. Government cuts and tech spending
  2. What's the deal with Japanese web design?
  3. Did the July PageRank update come early?
  4. Servers handling "Pending Delete" .COM domains failing
  5. Photoshop CS5, first impressions
  6. Google PageRank toolbar updates coming today
  7. To Microsoft's credit
  8. Tracking expiring and dropping domain names
  9. GoDaddy finally cleans up its checkout process
  10. Back to basics: clean up your link names

Welcome!

Wonder where to start with your web design business?

This blog follows along with my efforts to build and grow a website design business, Pro Content and Design.

The goal of this blog is to fill in blanks that may be empty as you get your business rolling.

This blog, particularly the source code section, is not intended for beginners. If you are not comfortable with databases, Ajax, DOM objects and other advanced methods, I strongly suggest you go take a look over at W3 Schools before even reading -- let alone tinkering with -- any of the code here.

I hope this blog has some value to web designers as they attempt to get their businesses going.

Good luck, and happy reading.

Thank you,
John Crawford
Pro Content and Design

Books


I highly recommend Art of the Start if you have no idea where to start with marketing.

Links

Coding
W3 Schools
IBM's Mastering Ajax Series

Graphic Design
Worth 1000
Stock.XCHNG
Urban Fonts

Website Software
Apache Web Server
SquirrelMail
PHP/Zend

Website Design Issues
Non-Standard Character Guide
Google Trends
Search Engine Optimization Analyzer

Business
Guy Kawasaki's Blog
Seth Godin's Blog
Freakonomics

Computers
NewEgg

My Main Website
Pro Content and Design

Websites I have built
PunxsyPage: local free classifieds website

Groundhog Festival: for the local summer festival

My Webapps
TV Stations Transmitter Database

Google PageRank Checker