mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-11 12:10:59 +08:00
Add testing for read_postgis function
It also installs postgis and creates a simple database filled with the borough data. A new requirements.test.txt file is created so that not everybody installing geopandas is required to install the packages for testing, psycopg2 in this case.
This commit is contained in:
@@ -7,6 +7,7 @@ import urllib2
|
||||
|
||||
import numpy as np
|
||||
from shapely.geometry import Point, Polygon
|
||||
import psycopg2
|
||||
|
||||
from geopandas import GeoDataFrame
|
||||
|
||||
@@ -82,3 +83,26 @@ class TestDataFrame(unittest.TestCase):
|
||||
lonlat = df2.to_crs(epsg=4326)
|
||||
utm = lonlat.to_crs(epsg=26918)
|
||||
self.assertTrue(all(df2['geometry'].almost_equals(utm['geometry'], decimal=2)))
|
||||
|
||||
def _validate_sql(self, df):
|
||||
# Make sure all the columns are there and the geometries
|
||||
# were properly loaded as MultiPolygons
|
||||
columns = ('borocode', 'boroname', 'shape_leng', 'shape_area')
|
||||
for col in columns:
|
||||
self.assertTrue(col in df.columns, 'Column {} missing'.format(col))
|
||||
self.assertTrue(all(df['geometry'].type == 'MultiPolygon'))
|
||||
|
||||
def test_read_postgis_default(self):
|
||||
con = psycopg2.connect(dbname='test_geopandas')
|
||||
sql = "SELECT * FROM nybb;"
|
||||
df = GeoDataFrame.read_postgis(sql, con)
|
||||
self._validate_sql(df)
|
||||
|
||||
def test_read_postgis_custom_geom_col(self):
|
||||
con = psycopg2.connect(dbname='test_geopandas')
|
||||
sql = """SELECT
|
||||
borocode, boroname, shape_leng, shape_area,
|
||||
geom AS __geometry__
|
||||
FROM nybb;"""
|
||||
df = GeoDataFrame.read_postgis(sql, con, geom_col='__geometry__')
|
||||
self._validate_sql(df)
|
||||
|
||||
Reference in New Issue
Block a user