Nighthawk2k14
Nighthawk2k14

Reputation: 21

How can i create GeoJSON for addressing it in an Layer URL by using geo data from postgres database?

I want to show administrative boundaries from a country on map with possability of user interaction(changing fillColor, show statistic data in a popup to a selected polygon, use events like onmouseover, (de-)select many polygons through on click and so on) on a Leaflet map with openstreetmap.

What i have done so far:

I wrote a script which shows those boundaries as polygons by request them by an user defined area. Those data can be very huge. Data from a whole country with all boundaries are many megabytes and so it is too slow to wait for in a standard ajax request with jQuery to an php script.

How i read data and stored it:

First i have read xml data from an osm file and stored it in an postgres database. boundaries(Relations from osm) are stored as geometry collection(Only polygons and multipolygons).

What i want to achieve:

Now i want to show all boundaries on map. On higher zoom levels it should show more boundaries from sub levels. So user can see sub areas of those he has seen on lower levels.

First level: Whole country in one polygon. Higher zoom levels: administrative boundaries of higher levels [...and so on...]

One possibility which seems to be an solution:

http://bl.ocks.org/glenrobertson/6203331

In that example is geojson data asked on server by client in the same way like map tiles.

var geojsonURL = 'http://polymaps.appspot.com/state/{z}/{x}/{y}.json';

So i could deliver different geojson on each zoom level and only those polygons i really need.

How can i transform data from geometry collection to geojson that can be adressed in that way?

An example would be very nice.

Upvotes: 1

Views: 902

Answers (1)

YaFred
YaFred

Reputation: 10008

Elements of answer ...

Delivering geojson data for an url like /{z}/{x}/{y}.json means you have to retrieve data pertaining to tile x,y in your zoom level z.

To know which latlng we are speaking of, you can use following php:

// SpericalMercator projection
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat._5
$n = pow(2, $zoom);
$lon1 = $xtile / $n * 360.0 - 180.0;
$lat1 = rad2deg(atan(sinh(pi() * (1 - 2 * $ytile / $n))));

$lon2 = ($xtile + 1) / $n * 360.0 - 180.0;
$lat2 = rad2deg(atan(sinh(pi() * (1 - 2 * ($ytile + 1) / $n))));

$SQL_where = "'latitude' <= $lat1 AND 'latitude' > $lat2 AND 'longitude' >= $lon1 AND 'longitude' < $lon2";

To retreive your data you will have to select your polygons having at least one point in the required tile.

Keep in mind that, as polygons may span over multiple tiles, you will return the same polygons in different responses. So you should name each polygon with a unique id so that you can deal with these duplicates on your client side

Reference: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

Upvotes: 0

Related Questions