Search code examples
postgresqlgislatitude-longitudepostgis

Can PostGIS be used to create a grid map of a country?


I'm trying to divide the map of an area into 1 km x 1 km square grids and ultimately, count and retrieve how many points (given their latitude and longitude values) fall into each square grid of this map. Can this operation be done in the PostGIS, if so, how can I do that?

UPDATE: Mike Toews has a detailed answer here:

https://gis.stackexchange.com/questions/16374/how-to-create-a-regular-polygon-grid-in-postgis


Solution

  • As mentioned in my comment make a regular grid. To make a 1 km grid for a whole country, this could be challenging, since the earth is not flat and cannot be divided into perfect 1 km grids.

    To make a 1 km grid, you need a projected coordinate system, with length units of metres. WGS84 (EPSG:4326) cannot do this, since it has units of degrees lat/long. To find a suitable projection system, you need to find an "equal area" projection, such as Lambert azimuthal equal-area projection (LAEA). For example all of Europe could use ETRS-LAEA (EPSG:3035), although there might be some distortion in some parts. Or if in New Zealand, New Zealand Transverse Mercator 2000. Each region generally has a good projection to use.

    To run your PostGIS query, you would need to project geometries onto the grid using ST_Transform(geom, 3035) (e.g., for ETRS-LAEA).