Ejemplo n.º 1
0
def test_geometric_types(postgres):
    adapter = PostgresAdapter(postgres.url(), 'geometric_test')
    adapter.field_shapes = {'path': 5}
    adapter.field_names = {4: 'path2'}
    expected = np.array([((1.1, 2.2),
                          [1, 2, 3],
                          [1, 2, 3, 4],
                          [3, 4, 1, 2],
                          [(1, 2), (3, 4), (5, 6), (0, 0), (0, 0)],
                          [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
                          [1, 2, 3])],
                        dtype=[(str('point'), 'f8', 2),
                               (str('line'), 'f8', 3),
                               (str('lseg'), 'f8', 4),
                               (str('box'), 'f8', 4), 
                               (str('path2'), 'f8', (5, 2)),
                               (str('polygon'), 'O'),
                               (str('circle'), 'f8', 3)])
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter.field_shapes = {'path2': 5}
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter.field_shapes = {4: 5}
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
Ejemplo n.º 2
0
def test_multipolygons(postgres):
    adapter = PostgresAdapter(postgres.url(),
        query='select polygon2d, polygon3d, polygon4d from multipolygons')
    adapter.field_shapes = {'polygon3d': (2, 4, 5), 'polygon4d': (2, 3, 4)}
    result = adapter[:]
    expected = np.array([('MULTIPOLYGON (((0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
                                         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
                                         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000)), '
                                        '((0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
                                         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
                                         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000)))',
                          [[[[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]],
                          [[[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
                           [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]],
                          [[[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)],
                           [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)],
                           [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)]],
                          [[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)],
                           [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)],
                           [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (0, 1, 2, 3)]]])],
                        dtype=[('polygon2d', 'O'),
                               ('polygon3d', 'f8', (2, 4, 5, 3)),
                               ('polygon4d', 'f8', (2, 3, 4, 4))])
    np.testing.assert_array_equal(expected, result)
Ejemplo n.º 3
0
def test_missing_values(postgres):
    # Don't test missing values for PostGIS types for now. Since PostGIS type metadata
    # is stored by postgresql as actual data in the record, an empty or missing
    # value in a PostGIS column contains no metadata about what type it
    # actually is (and postgresql doesn't know about GIS types so doesn't
    # store that column metadata anywhere). In order to handle missing data
    # for PostGIS types, we'll probably need to come up with some sort of
    # generic PostGIS object or dtype which can be set to NULL for missing data.
    adapter = PostgresAdapter(postgres.url(), table='missing_values_test')
    adapter.field_shapes = {'path': 2}
    result = adapter[:]
    expected = np.array([('', 0, np.nan, [np.nan, np.nan], [(np.nan, np.nan), (np.nan, np.nan)], [])],
        dtype=[(str('char'), str('U5')),
               (str('int4'), str('i4')),
               (str('float4'), str('f4')),
               (str('point'), str('f8'), 2),
               (str('path'), str('f8'), (2, 2)),
               (str('polygon'), str('O'))])
    assert expected.dtype == result.dtype
    assert result[0][0] == ''
    assert result[0][1] == 0
    assert np.isnan(result[0][2])
    assert np.isnan(result[0][3][0])
    assert np.isnan(result[0][3][1])
    assert np.isnan(result[0][4][0][0])
    assert np.isnan(result[0][4][0][1])
    assert np.isnan(result[0][4][1][0])
    assert np.isnan(result[0][4][1][1])
    assert len(result[0][5]) == 0
Ejemplo n.º 4
0
def test_multilines(postgres):
    adapter = PostgresAdapter(postgres.url(),
        query='select line2d, line3d, line4d from multilines')
    adapter.field_shapes = {'line3d': (2, 3), 'line4d': (2, 2)}
    result = adapter[:]
    expected = np.array([('MULTILINESTRING ((0.000000 1.000000, 2.000000 3.000000), '
                                           '(4.000000 5.000000, 6.000000 7.000000))',
                          [[[0, 1, 2], [3, 4, 5], [0, 0, 0]], [[6, 7, 8], [9, 10, 11], [12, 13, 14]]],
                          [[(0, 1, 2, 3), (4, 5, 6, 7)], [(8, 9, 10, 11), (12, 13, 14, 15)]])],
                        dtype=[('line2d', 'O'),
                               ('line3d', 'f8', (2, 3, 3)),
                               ('line4d', 'f8', (2, 2, 4))])
    np.testing.assert_array_equal(expected, result)
Ejemplo n.º 5
0
def test_multipoints(postgres):
    adapter = PostgresAdapter(postgres.url(),
        query='select point2d, point3d, point4d from multipoints')
    adapter.field_shapes = {'point2d': 1, 'point3d': 4}
    result = adapter[:]
    expected = np.array([([[0, 1]],
                          [[0, 1, 2], [3, 4, 5], [0, 0, 0], [0, 0, 0]],
                          'MULTIPOINT ((0.000000 1.000000 2.000000 3.000000), '
                                      '(4.000000 5.000000 6.000000 7.000000))')],
                        dtype=[('point2d', 'f8', (1, 2)),
                               ('point3d', 'f8', (4, 3)),
                               ('point4d', 'O')])
    np.testing.assert_array_equal(expected, result)
Ejemplo n.º 6
0
def test_lines(postgres):
    adapter = PostgresAdapter(postgres.url(),
        query='select line2d, line3d, line4d from lines')
    adapter.field_shapes = {'line2d': 1, 'line3d': 3}
    result = adapter[:]
    expected = np.array([([[0.0, 1.0]],
                          [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [0.0, 0.0, 0.0]],
                          'LINESTRING (0.000000 1.000000 2.000000 3.000000, '
                                      '4.000000 5.000000 6.000000 7.000000)'),
                         ([[0.0, 1.0]],
                          [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
                          'LINESTRING (0.000000 1.000000 2.000000 3.000000, '
                                      '4.000000 5.000000 6.000000 7.000000)')],
                        dtype=[('line2d', 'f8', (1,2)),
                               ('line3d', 'f8', (3,3)),
                               ('line4d', 'O')])
    np.testing.assert_array_equal(expected, result)
Ejemplo n.º 7
0
 def test_polygons(self):
     adapter = PostgresAdapter(
         self.postgresql_url,
         query='select polygon2d, polygon3d, polygon4d from polygons')
     adapter.field_shapes = {'polygon3d': (4, 5), 'polygon4d': (3, 4)}
     result = adapter[:]
     expected = np.array([(
         'POLYGON ((0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000), '
         '(0.000000 1.000000, 2.000000 3.000000, 4.000000 5.000000, 0.000000 1.000000))',
         [[[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
          [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
          [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [0, 0, 0]],
          [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
          ], [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [0, 1, 2, 3]],
              [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [0, 1, 2, 3]],
              [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [0, 1, 2, 3]]])],
                         dtype=[('polygon2d', 'O'),
                                ('polygon3d', 'f8', (4, 5, 3)),
                                ('polygon4d', 'f8', (3, 4, 4))])
     np.testing.assert_array_equal(expected, result)
Ejemplo n.º 8
0
def test_dataframe(postgres):
    adapter = PostgresAdapter(postgres.url(), table='ints_test', dataframe=True)
    expected = pd.DataFrame({'int2': np.array([np.iinfo(np.int16).min, 0, np.iinfo(np.int16).max], dtype='i2'),
                             'int4': np.array([np.iinfo(np.int32).min, 0, np.iinfo(np.int32).max], dtype='i4'),
                             'int8': np.array([np.iinfo(np.int64).min, 0, np.iinfo(np.int64).max], dtype='i8')})
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
    
    adapter = PostgresAdapter(postgres.url(), 'casts_test', dataframe=True)
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('char'), str('O')),
                                                          (str('int4'), str('i4')),
                                                          (str('float8'), str('f8'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (str(i).ljust(10), i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'casts_test', dataframe=True, field_filter=['int4', 'float8'])
    adapter.field_types = ['i2', 'f4']
    adapter.field_names = ['a', 'b']
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('i2')), (str('b'), str('f4'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter.field_types = {'a': 'f4'}
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('f4')), (str('b'), str('f8'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(),
                              'geometric_test',
                              dataframe=True,
                              field_filter=['point', 'line', 'polygon'])
    result = adapter[:]
    point_data = np.empty(1, dtype='O')
    point_data[0] = [1.1, 2.2]
    line_data = np.empty(1, dtype='O')
    line_data[0] = [1.0, 2.0, 3.0]
    polygon_data = np.empty(1, dtype='O')
    polygon_data[0] = [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]
    expected = pd.DataFrame(OrderedDict([('point', point_data),
                                         ('line', line_data),
                                         ('polygon', polygon_data)]),
                                        index=np.array([0], dtype='u8'))
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'fixed_strings_test', dataframe=True)
    result = adapter[:]
    expected = pd.DataFrame(['aaa       ', 'bbb       ', 'ccc       '],
                            columns=['fixed'],
                            index=np.array([0, 1, 2], dtype='u8'))
    assert_frame_equal(expected, result)

    with pytest.raises(RuntimeError):
        adapter.field_shapes = {'fixed': 2}