如何在 GeoJSON 中转换 SDO_GEOMTRY
How to convert SDO_GEOMTRY in GeoJSON
我使用 sqlalchemy 和 geoalchemy,并将我的结果转换为 geojson。
像这样的正常方式:
print json.dumps([dict(r) for r in connection.execute(query)])
不可能,因为cx_Oracle.Objets不可序列化!
我可以通过这样的单独属性进行访问:
result = connection.execute(query)
result2 = result.fetchone()[0]
print result2.SDO_ORDINATES
这是我的程序:
#!/usr/bin/env python
# coding: utf8
#from __future__ import absolute_import, division, print_function
from sqlalchemy import create_engine
from sqlalchemy import Table, MetaData
from sqlalchemy.sql import and_, select
from geoalchemy import Geometry, GeometryExtensionColumn
from geoalchemy import *
from geoalchemy.oracle import oracle_functions
from geoalchemy.oracle import OracleComparator
import cx_Oracle
import json
import sdo
#def main():
engine = create_engine('oracle+cx_oracle://TEST_3D:limo1013@10.40.33.160:1521/sdetest')
metadata = MetaData(engine)
# Loading tables
building = Table(
'building',
metadata,
GeometryExtensionColumn('centroid_geom', Geometry(2, srid= 431467)),
autoload=True,
autoload_with=engine
)
GeometryDDL(building)
thematic_surface = Table('thematic_surface', metadata, autoload=True)
surface_geometry = Table('surface_geometry', metadata, autoload=True)
objectclass = Table('objectclass', metadata, autoload=True)
connection = engine.connect()
# define the query
query = select([(surface_geometry.c.geometry)] #building.c.id, surface_geometry.c.geometry, objectclass.c.classname
).where(
and_(
building.c.grid_id_400 == 4158,
building.c.id == thematic_surface.c.building_id,
thematic_surface.c.lod2_multi_surface_id == surface_geometry.c.root_id,
surface_geometry.c.geometry != None,
thematic_surface.c.objectclass_id == objectclass.c.id,
)
)
# Execute and print the result of the query
#print json.dumps([dict(r) for r in connection.execute(query)])
result = connection.execute(query)
我会将我所有的 cx_Oracle.Objects 转换为 GeoJSON,但如何转换?
在 Internet 上,有一个函数 sdo2geojson 在 sql 开发人员中运行良好,但当然这个函数对于 python...
是未知的
希望有人能帮帮我???
这是使用 cx_Oracle 的(尚未发布的)版本,它支持对象绑定和对象的其他更高级用途。使用 cx_Oracle 提供的示例来演示几何体的插入,以下代码会将以这种方式创建的对象转换为 JSON。下面包含的 ObjectRepr() 函数应该适用于从 Oracle 返回的任何对象。它只是读取对象上的元数据并将对象转换为属性字典或值列表。
import cx_Oracle
import json
connection = cx_Oracle.Connection("user/pw@tns")
typeObj = connection.gettype("SDO_GEOMETRY")
cursor = connection.cursor()
cursor.execute("""
select Geometry
from TestGeometry
where IntCol = 1""")
obj, = cursor.fetchone()
def ObjectRepr(obj):
if obj.type.iscollection:
returnValue = []
for value in obj.aslist():
if isinstance(value, cx_Oracle.Object):
value = ObjectRepr(value)
returnValue.append(value)
else:
returnValue = {}
for attr in obj.type.attributes:
value = getattr(obj, attr.name)
if value is None:
continue
elif isinstance(value, cx_Oracle.Object):
value = ObjectRepr(value)
returnValue[attr.name] = value
return returnValue
print("JSON:", json.dumps(ObjectRepr(obj)))
我使用 sqlalchemy 和 geoalchemy,并将我的结果转换为 geojson。 像这样的正常方式:
print json.dumps([dict(r) for r in connection.execute(query)])
不可能,因为cx_Oracle.Objets不可序列化! 我可以通过这样的单独属性进行访问:
result = connection.execute(query)
result2 = result.fetchone()[0]
print result2.SDO_ORDINATES
这是我的程序:
#!/usr/bin/env python
# coding: utf8
#from __future__ import absolute_import, division, print_function
from sqlalchemy import create_engine
from sqlalchemy import Table, MetaData
from sqlalchemy.sql import and_, select
from geoalchemy import Geometry, GeometryExtensionColumn
from geoalchemy import *
from geoalchemy.oracle import oracle_functions
from geoalchemy.oracle import OracleComparator
import cx_Oracle
import json
import sdo
#def main():
engine = create_engine('oracle+cx_oracle://TEST_3D:limo1013@10.40.33.160:1521/sdetest')
metadata = MetaData(engine)
# Loading tables
building = Table(
'building',
metadata,
GeometryExtensionColumn('centroid_geom', Geometry(2, srid= 431467)),
autoload=True,
autoload_with=engine
)
GeometryDDL(building)
thematic_surface = Table('thematic_surface', metadata, autoload=True)
surface_geometry = Table('surface_geometry', metadata, autoload=True)
objectclass = Table('objectclass', metadata, autoload=True)
connection = engine.connect()
# define the query
query = select([(surface_geometry.c.geometry)] #building.c.id, surface_geometry.c.geometry, objectclass.c.classname
).where(
and_(
building.c.grid_id_400 == 4158,
building.c.id == thematic_surface.c.building_id,
thematic_surface.c.lod2_multi_surface_id == surface_geometry.c.root_id,
surface_geometry.c.geometry != None,
thematic_surface.c.objectclass_id == objectclass.c.id,
)
)
# Execute and print the result of the query
#print json.dumps([dict(r) for r in connection.execute(query)])
result = connection.execute(query)
我会将我所有的 cx_Oracle.Objects 转换为 GeoJSON,但如何转换? 在 Internet 上,有一个函数 sdo2geojson 在 sql 开发人员中运行良好,但当然这个函数对于 python...
是未知的希望有人能帮帮我???
这是使用 cx_Oracle 的(尚未发布的)版本,它支持对象绑定和对象的其他更高级用途。使用 cx_Oracle 提供的示例来演示几何体的插入,以下代码会将以这种方式创建的对象转换为 JSON。下面包含的 ObjectRepr() 函数应该适用于从 Oracle 返回的任何对象。它只是读取对象上的元数据并将对象转换为属性字典或值列表。
import cx_Oracle
import json
connection = cx_Oracle.Connection("user/pw@tns")
typeObj = connection.gettype("SDO_GEOMETRY")
cursor = connection.cursor()
cursor.execute("""
select Geometry
from TestGeometry
where IntCol = 1""")
obj, = cursor.fetchone()
def ObjectRepr(obj):
if obj.type.iscollection:
returnValue = []
for value in obj.aslist():
if isinstance(value, cx_Oracle.Object):
value = ObjectRepr(value)
returnValue.append(value)
else:
returnValue = {}
for attr in obj.type.attributes:
value = getattr(obj, attr.name)
if value is None:
continue
elif isinstance(value, cx_Oracle.Object):
value = ObjectRepr(value)
returnValue[attr.name] = value
return returnValue
print("JSON:", json.dumps(ObjectRepr(obj)))