opentraveldata / geobases

Data services and visualization
http://opentraveldata.github.com/geobases/
Other
193 stars 41 forks source link

example on how to display a shape #4

Closed kedmenec closed 11 years ago

kedmenec commented 11 years ago

How do I overlay a shape (eg polygon) by specifying coordinates in lat long format?

alexprengere commented 11 years ago

I guess your question is a mix of "I want to use my own data" and "I want to draw". I am going to give you a complete answer, but before you may be interested by these two links:

So to display a polygon on a map using lat/lng input, you first want to create the GeoBase with your own lat/lng, not the ones provided through the different embedded data sources.

Suppose you have a file containing three columns like that:

$ cat file.csv
d1 48.22 2.33
d2 49.33 2.24
d3 49.33 2.47

Create the GeoBase object, then use the visualize method:

from GeoBases import GeoBase

g = GeoBase(data='feed', 
            source=open('file.csv'),
            delimiter=' ',
            headers=['name', 'lat', 'lng'],
            indexes = ['name'])

lines = [
    ['d1', 'd2'],
    ['d2', 'd3'],
    ['d3', 'd1'],
]

g.visualize(output='name', add_lines=lines) 

Perhaps you did not want to create a file.csv, it's ok! The input for source just has to be an iterable of lines, so you may use something like this instead:

g = GeoBase(data='feed', 
            source=[
                'd1 48.22 2.33',
                'd2 49.33 2.24',
                'd3 49.33 2.47'
            ],
            delimiter=' ',
            headers=['name', 'lat', 'lng'],
            indexes = ['name'])

You may even create an empty object with g = GeoBase('feed') and then set each key with g.set('d1', 'lat', 48.2) and g.set('d1', 'lng', 2.4).

I hope I answered your question. If you only want to use the command line, the only thing you may do is:

$ cat file.csv | GeoBase --map

That will create a GeoBase object from the stdin input, but you will not be able to configure manually the lines displayed (though if you click on Link all the markers will be linked).

On the development branch, we are working on map enhancements which would allow to directly give additional input to the visualize method, as you may see on that gist, but this is not stable yet and the API may still change.