| 
 | 1 | +#!/usr/bin/env python  | 
 | 2 | +# -*- encoding: utf-8 -*-  | 
 | 3 | + | 
 | 4 | +# Copyright (c) "Neo4j"  | 
 | 5 | +# Neo4j Sweden AB [http://neo4j.com]  | 
 | 6 | +#  | 
 | 7 | +# This file is part of Neo4j.  | 
 | 8 | +#  | 
 | 9 | +# Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 10 | +# you may not use this file except in compliance with the License.  | 
 | 11 | +# You may obtain a copy of the License at  | 
 | 12 | +#  | 
 | 13 | +#     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 14 | +#  | 
 | 15 | +# Unless required by applicable law or agreed to in writing, software  | 
 | 16 | +# distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 18 | +# See the License for the specific language governing permissions and  | 
 | 19 | +# limitations under the License.  | 
 | 20 | + | 
 | 21 | +import pytest  | 
 | 22 | + | 
 | 23 | +# python -m pytest tests/integration/examples/test_geospatial_types_example.py -s -v  | 
 | 24 | + | 
 | 25 | + | 
 | 26 | +def _echo(tx, x):  | 
 | 27 | +    return tx.run("RETURN $x AS fieldName", x=x).single()  | 
 | 28 | + | 
 | 29 | + | 
 | 30 | +def test_cartesian_point(driver):  | 
 | 31 | +    # tag::geospatial-types-cartesian-import[]  | 
 | 32 | +    from neo4j.spatial import CartesianPoint  | 
 | 33 | +    # end::geospatial-types-cartesian-import[]  | 
 | 34 | + | 
 | 35 | +    # tag::geospatial-types-cartesian[]  | 
 | 36 | +    # Creating a 2D point in Cartesian space  | 
 | 37 | +    point2d = CartesianPoint((1, 5.1))  | 
 | 38 | +    # Or in 3D  | 
 | 39 | +    point3d = CartesianPoint((1, -2., 3.1))  | 
 | 40 | +    # end::geospatial-types-cartesian[]  | 
 | 41 | + | 
 | 42 | +    # storing points for later assertions  | 
 | 43 | +    in_point2d = point2d  | 
 | 44 | +    in_point3d = point3d  | 
 | 45 | + | 
 | 46 | +    with driver.session() as session:  | 
 | 47 | +        record_with_2d_point = session.read_transaction(_echo, point2d)  | 
 | 48 | +        record_with_3d_point = session.read_transaction(_echo, point3d)  | 
 | 49 | + | 
 | 50 | +    # tag::geospatial-types-cartesian[]  | 
 | 51 | + | 
 | 52 | +    # Reading a 2D point from a record  | 
 | 53 | +    point2d = record_with_2d_point.get("fieldName")  # type: CartesianPoint  | 
 | 54 | +    str(point2d)  # POINT(1.0 5.1)  | 
 | 55 | +    point2d.x  # 1.0  | 
 | 56 | +    point2d.y  # 5.1  | 
 | 57 | +    # point2d.z raises AttributeError  | 
 | 58 | +    point2d.srid  # 7203  | 
 | 59 | +    len(point2d)  # 2  | 
 | 60 | + | 
 | 61 | +    # Reading a 3D point from a record  | 
 | 62 | +    point3d = record_with_3d_point.get("fieldName")  # type: CartesianPoint  | 
 | 63 | +    str(point3d)  # POINT(1.0 -2.0 3.1)  | 
 | 64 | +    point3d.x  # 1.0  | 
 | 65 | +    point3d.y  # -2.0  | 
 | 66 | +    point3d.z  # 3.1  | 
 | 67 | +    point3d.srid  # 9157  | 
 | 68 | +    len(point2d)  # 3  | 
 | 69 | +    # end::geospatial-types-cartesian[]  | 
 | 70 | + | 
 | 71 | +    assert str(point2d) == "POINT(1.0 5.1)"  | 
 | 72 | +    assert isinstance(point2d.x, float) and point2d.x == 1.0  | 
 | 73 | +    assert isinstance(point2d.y, float) and point2d.y == 5.1  | 
 | 74 | +    with pytest.raises(AttributeError):  | 
 | 75 | +        point2d.z  | 
 | 76 | +    assert point2d.srid == 7203  | 
 | 77 | +    assert len(point2d) == 2  | 
 | 78 | +    assert point2d == in_point2d  | 
 | 79 | + | 
 | 80 | +    assert str(point3d) == "POINT(1.0 -2.0 3.1)"  | 
 | 81 | +    assert isinstance(point3d.x, float) and point3d.x == 1.0  | 
 | 82 | +    assert isinstance(point3d.y, float) and point3d.y == -2.0  | 
 | 83 | +    assert isinstance(point3d.z, float) and point3d.z == 3.1  | 
 | 84 | +    assert point3d.srid == 9157  | 
 | 85 | +    assert len(point3d) == 3  | 
 | 86 | +    assert point3d == in_point3d  | 
 | 87 | + | 
 | 88 | + | 
 | 89 | +def test_wgs84_point(driver):  | 
 | 90 | +    # tag::geospatial-types-wgs84-import[]  | 
 | 91 | +    from neo4j.spatial import WGS84Point  | 
 | 92 | +    # end::geospatial-types-wgs84-import[]  | 
 | 93 | + | 
 | 94 | +    # tag::geospatial-types-wgs84[]  | 
 | 95 | +    # Creating a 2D point in WSG84 space  | 
 | 96 | +    point2d = WGS84Point((1, 5.1))  | 
 | 97 | +    # Or in 3D  | 
 | 98 | +    point3d = WGS84Point((1, -2., 3.1))  | 
 | 99 | +    # end::geospatial-types-wgs84[]  | 
 | 100 | + | 
 | 101 | +    # storing points for later assertions  | 
 | 102 | +    in_point2d = point2d  | 
 | 103 | +    in_point3d = point3d  | 
 | 104 | + | 
 | 105 | +    with driver.session() as session:  | 
 | 106 | +        record_with_2d_point = session.read_transaction(_echo, point2d)  | 
 | 107 | +        record_with_3d_point = session.read_transaction(_echo, point3d)  | 
 | 108 | + | 
 | 109 | +    # tag::geospatial-types-wgs84[]  | 
 | 110 | + | 
 | 111 | +    # Reading a 2D point from a record  | 
 | 112 | +    point2d = record_with_2d_point.get("fieldName")  # type: WGS84Point  | 
 | 113 | +    str(point2d)  # POINT(1.0 5.1)  | 
 | 114 | +    point2d.longitude  # 1.0 (point2d.x is an alias for longitude)  | 
 | 115 | +    point2d.latitude  # 5.1 (point2d.y is an alias for latitude)  | 
 | 116 | +    # point2d.height raises AttributeError (same with point2d.z)  | 
 | 117 | +    point2d.srid  # 4326  | 
 | 118 | +    len(point2d)  # 2  | 
 | 119 | + | 
 | 120 | +    # Reading a 3D point from a record  | 
 | 121 | +    point3d = record_with_3d_point.get("fieldName")  # type: WGS84Point  | 
 | 122 | +    str(point3d)  # POINT(1.0 -2.0 3.1)  | 
 | 123 | +    point3d.longitude  # 1.0 (point3d.x is an alias for longitude)  | 
 | 124 | +    point3d.latitude  # -2.0 (point3d.y is an alias for latitude)  | 
 | 125 | +    point3d.height  # 3.1 (point3d.z is an alias for height)  | 
 | 126 | +    point3d.srid  # 4979  | 
 | 127 | +    len(point2d)  # 3  | 
 | 128 | +    # end::geospatial-types-wgs84[]  | 
 | 129 | + | 
 | 130 | +    assert str(point2d) == "POINT(1.0 5.1)"  | 
 | 131 | +    assert isinstance(point2d.longitude, float) and point2d.longitude == 1.0  | 
 | 132 | +    assert isinstance(point2d.x, float) and point2d.x == 1.0  | 
 | 133 | +    assert isinstance(point2d.latitude, float) and point2d.latitude == 5.1  | 
 | 134 | +    assert isinstance(point2d.y, float) and point2d.y == 5.1  | 
 | 135 | +    with pytest.raises(AttributeError):  | 
 | 136 | +        point2d.height  | 
 | 137 | +    with pytest.raises(AttributeError):  | 
 | 138 | +        point2d.z  | 
 | 139 | +    assert point2d.srid == 4326  | 
 | 140 | +    assert len(point2d) == 2  | 
 | 141 | +    assert point2d == in_point2d  | 
 | 142 | + | 
 | 143 | +    assert str(point3d) == "POINT(1.0 -2.0 3.1)"  | 
 | 144 | +    assert isinstance(point3d.longitude, float) and point3d.longitude == 1.0  | 
 | 145 | +    assert isinstance(point3d.x, float) and point3d.x == 1.0  | 
 | 146 | +    assert isinstance(point3d.latitude, float) and point3d.latitude == -2.0  | 
 | 147 | +    assert isinstance(point3d.y, float) and point3d.y == -2.0  | 
 | 148 | +    assert isinstance(point3d.height, float) and point3d.height == 3.1  | 
 | 149 | +    assert isinstance(point3d.z, float) and point3d.z == 3.1  | 
 | 150 | +    assert point3d.srid == 4979  | 
 | 151 | +    assert len(point3d) == 3  | 
 | 152 | +    assert point3d == in_point3d  | 
0 commit comments