This is what I want: when I zoom out, cities of the country should appear as points and at a certain level when I zoom in, I want them to appear as polygons (showing the area of the city and boundaries). How can I do that? I am thinking of creating two tables "cities_points" and "cities_polygons" in my PostGIS database. Any idea? (actually not only the cities, but it's the same for other components of my map).

  • What geometry type do you currently have in your table? – Scro Apr 22 '12 at 16:08
  • -------Polygons – YossBen Apr 22 '12 at 17:50
  • It seems the eventual solution to this problem would also be a mechanism for returning more or less-generalized geometries as a map client's scale decreases or increases, respectively. +1 – elrobis Apr 23 '12 at 13:23

You can take advantage of layer groups in geoserver.

Having in PostGIS the two geometries in separated tables, just create a layer group in geoserver with both layers and by means of SLD styling set the scale under which every layer should be rendered (use maxscaledenominator & minscaledenominator parameters).

  • Indeed, I think it's better to use Geoserver layers grouping option and zoom configuration possibilities that come with it rather than publishing each layer (points & polygons) independently and configuring their zoom level with OpenLayers. Ur answer is exactly the same as @atlefren :) So I posted the same comment. Thanx anyway.. – YossBen Apr 23 '12 at 10:45

One approach would be to create a view of the polygon centroids and manually add it to the geometry_columns table. The benifit of this method is that there is practically no maintenance of the points table as it will show any updates to the polygon table and make use of any of its indexes. The view creation would look something like this:

CREATE VIEW the_schema.city_points AS
SELECT id, foo, bar, etc, ST_Centroid(the_geom);

Then add it to the geometry_columns table, like so:

INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
SELECT '', 'the_schema', 'city_points', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom)
FROM the_schema.city_points LIMIT 1;

Hope that helps.

  • Sure it helps! But once the points are created and related to the polygons, I need to know how to display one layer (one layer=one table or view) instead of other depending on zoom level. I am quite sure that this is possible with OpenLayers, but haven't found it yet. – YossBen Apr 22 '12 at 18:49
  • Sorry, I don't know OpenLayers. I just saw "two tables" and panicked. Hard to imagine that you can't set zoom levels with OpenLayers, and the following link appears to say that you can: trac.osgeo.org/openlayers/wiki/SettingZoomLevels – Scro Apr 22 '12 at 19:06
  • That's exactly what I am reading right now :) – YossBen Apr 22 '12 at 19:07

Which technology (apart from PostGIS, obviously) do you use? Is it a web application? If so, I'd recommend using Leaflet.js with a GeoJSON overlay from a server resource which returns either points or polygons depending on the zoom level you query the resource with. I can be more specific if i know the details.

  • Hi! Beside PosGIS, I am using GeoServer and on the client side OpenLayers along with GeoExt.. – YossBen Apr 22 '12 at 16:14
  • Assuming I use Leaflet as u proposed, will I need two tables, one of cities as points and other as polygons, or I'll need just one table of cities as polygons and then maybe Leaflet or the server will convert the polygon into a point (by doing some calculations like the center of the polygon) depending on the zoom level? I hope there is a similar option in GeoExt or a connexion between the two JS libraries because I've done much until now with GeoExt and don't have time to go back :) – YossBen Apr 22 '12 at 16:23
  • By the way, GeoServer can return results in GeoJSON format.. – YossBen Apr 22 '12 at 16:27
  • You would need just the polygon layer. Depending on the zoom level, the server side query would either return the polygons oder do ST_Centroid() on them. I am not familiar with GeoServer i'm afraid. You would have to create a resource responding with GeoJSON to a query of a) the bounding box of the current map view and b) the zoom level which is requested on every interaction the user does with the map (zoom, drag). – chriserik Apr 22 '12 at 16:31
  • Thanx buddy..Hope I'll find that ST_centroid() or equivalent in GeoServer.. – YossBen Apr 22 '12 at 16:52

Why not display the points/polygons as a wms-layer? Create a view as Scro suggested, create a layer for points and a layer for polygons in geoserver, set appropriate max- and minscalethresholds in sld-files, and combine them in a layer group.

  • Indeed, I think it's better to use Geoserver layers grouping option and zoom configuration possibilities that come with it rather than publishing each layer (points & polygons) independently and configuring their zoom level with OpenLayers. – YossBen Apr 23 '12 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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