# Path: ogrtools/pyogr/ogrvrt.py
# def ogr2vrt(infile,
#             outfile=None,
#             layer_list=[],
#             relative="0",
#             schema=0):
# 
#     ##########################################################################
#     # Open the datasource to read.
# 
#     src_ds = ogr.Open(infile, update=0)
# 
#     if src_ds is None:
#         return None
# 
#     if schema:
#         infile = '@dummy@'
# 
#     if len(layer_list) == 0:
#         for layer in src_ds:
#             layer_list.append(layer.GetLayerDefn().GetName())
# 
#     ##########################################################################
#     # Start the VRT file.
# 
#     vrt = '<OGRVRTDataSource>\n'
# 
#     ##########################################################################
#     #	Process each source layer.
# 
#     for name in layer_list:
#         layer = src_ds.GetLayerByName(name)
#         layerdef = layer.GetLayerDefn()
# 
#         vrt += '  <OGRVRTLayer name="%s">\n' % Esc(name)
#         vrt += '    <SrcDataSource relativeToVRT="%s" shared="%d">%s</SrcDataSource>\n' \
#                % (relative, not schema, Esc(infile))
#         if schema:
#             vrt += '    <SrcLayer>@dummy@</SrcLayer>\n'
#         else:
#             vrt += '    <SrcLayer>%s</SrcLayer>\n' % Esc(name)
#         vrt += '    <GeometryType>%s</GeometryType>\n' \
#                % GeomType2Name(layerdef.GetGeomType())
#         srs = layer.GetSpatialRef()
#         if srs is not None:
#             vrt += '    <LayerSRS>%s</LayerSRS>\n' \
#                    % (Esc(srs.ExportToWkt()))
# 
#         # Process all the fields.
#         for fld_index in range(layerdef.GetFieldCount()):
#             src_fd = layerdef.GetFieldDefn(fld_index)
#             ftype = ogr.GetFieldTypeName(src_fd.GetType())
#             vrt += '    <Field name="%s" type="%s"' \
#                    % (Esc(src_fd.GetName()), ftype)
#             if not schema:
#                 vrt += ' src="%s"' % Esc(src_fd.GetName())
#             if src_fd.GetWidth() > 0:
#                 vrt += ' width="%d"' % src_fd.GetWidth()
#             if src_fd.GetPrecision() > 0:
#                 vrt += ' precision="%d"' % src_fd.GetPrecision()
#             vrt += '/>\n'
# 
#         vrt += '  </OGRVRTLayer>\n'
# 
#     vrt += '</OGRVRTDataSource>\n'
# 
#     if outfile is not None:
#         f = open(outfile, "w")
#         f.write(vrt)
#         f.close()
# 
#     return vrt


from ogrtools.pyogr.ogrvrt import ogr2vrt


def test_ogrvrt():
    expected = """<OGRVRTDataSource>
  <OGRVRTLayer name="railway">
    <SrcDataSource relativeToVRT="0" shared="1">tests/data/osm/railway.shp</SrcDataSource>
    <SrcLayer>railway</SrcLayer>
    <GeometryType>wkbLineString</GeometryType>
    <LayerSRS>GEOGCS[&quot;GCS_WGS_1984&quot;,DATUM[&quot;WGS_1984&quot;,SPHEROID[&quot;WGS_84&quot;,6378137,298.257223563]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;Degree&quot;,0.017453292519943295]]</LayerSRS>
    <Field name="type" type="String" src="type" width="255"/>
    <Field name="osm_id" type="Integer64" src="osm_id" width="11"/>
    <Field name="lastchange" type="Date" src="lastchange" width="10"/>
    <Field name="name" type="String" src="name" width="255"/>
    <Field name="keyvalue" type="String" src="keyvalue" width="80"/>
  </OGRVRTLayer>
</OGRVRTDataSource>
"""

# Path: ogrtools/pyogr/ogrvrt.py
# def ogr2vrt(infile,
#             outfile=None,
#             layer_list=[],
#             relative="0",
#             schema=0):
# 
#     ##########################################################################
#     # Open the datasource to read.
# 
#     src_ds = ogr.Open(infile, update=0)
# 
#     if src_ds is None:
#         return None
# 
#     if schema:
#         infile = '@dummy@'
# 
#     if len(layer_list) == 0:
#         for layer in src_ds:
#             layer_list.append(layer.GetLayerDefn().GetName())
# 
#     ##########################################################################
#     # Start the VRT file.
# 
#     vrt = '<OGRVRTDataSource>\n'
# 
#     ##########################################################################
#     #	Process each source layer.
# 
#     for name in layer_list:
#         layer = src_ds.GetLayerByName(name)
#         layerdef = layer.GetLayerDefn()
# 
#         vrt += '  <OGRVRTLayer name="%s">\n' % Esc(name)
#         vrt += '    <SrcDataSource relativeToVRT="%s" shared="%d">%s</SrcDataSource>\n' \
#                % (relative, not schema, Esc(infile))
#         if schema:
#             vrt += '    <SrcLayer>@dummy@</SrcLayer>\n'
#         else:
#             vrt += '    <SrcLayer>%s</SrcLayer>\n' % Esc(name)
#         vrt += '    <GeometryType>%s</GeometryType>\n' \
#                % GeomType2Name(layerdef.GetGeomType())
#         srs = layer.GetSpatialRef()
#         if srs is not None:
#             vrt += '    <LayerSRS>%s</LayerSRS>\n' \
#                    % (Esc(srs.ExportToWkt()))
# 
#         # Process all the fields.
#         for fld_index in range(layerdef.GetFieldCount()):
#             src_fd = layerdef.GetFieldDefn(fld_index)
#             ftype = ogr.GetFieldTypeName(src_fd.GetType())
#             vrt += '    <Field name="%s" type="%s"' \
#                    % (Esc(src_fd.GetName()), ftype)
#             if not schema:
#                 vrt += ' src="%s"' % Esc(src_fd.GetName())
#             if src_fd.GetWidth() > 0:
#                 vrt += ' width="%d"' % src_fd.GetWidth()
#             if src_fd.GetPrecision() > 0:
#                 vrt += ' precision="%d"' % src_fd.GetPrecision()
#             vrt += '/>\n'
# 
#         vrt += '  </OGRVRTLayer>\n'
# 
#     vrt += '</OGRVRTDataSource>\n'
# 
#     if outfile is not None:
#         f = open(outfile, "w")
#         f.write(vrt)
#         f.close()
# 
#     return vrt


# Path: tests/test_ogrvrt.py
from ogrtools.pyogr.ogrvrt import ogr2vrt



def test_ogrvrt():
    expected = """<OGRVRTDataSource>
  <OGRVRTLayer name="railway">
    <SrcDataSource relativeToVRT="0" shared="1">tests/data/osm/railway.shp</SrcDataSource>
    <SrcLayer>railway</SrcLayer>
    <GeometryType>wkbLineString</GeometryType>
    <LayerSRS>GEOGCS[&quot;GCS_WGS_1984&quot;,DATUM[&quot;WGS_1984&quot;,SPHEROID[&quot;WGS_84&quot;,6378137,298.257223563]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;Degree&quot;,0.017453292519943295]]</LayerSRS>
    <Field name="type" type="String" src="type" width="255"/>
    <Field name="osm_id" type="Integer64" src="osm_id" width="11"/>
    <Field name="lastchange" type="Date" src="lastchange" width="10"/>
    <Field name="name" type="String" src="name" width="255"/>
    <Field name="keyvalue" type="String" src="keyvalue" width="80"/>
  </OGRVRTLayer>
</OGRVRTDataSource>
"""

    vrt = ogr2vrt(infile="tests/data/osm/railway.shp")