| 39 | == How do I translate a mixed geometry file to shapefile format? == |
| 40 | |
| 41 | Some formats (such as ESRI Shapefiles) only allow one type of geometry in |
| 42 | a layer, while other formats (such as DGN, MapInfo, GML) allow a mixture of |
| 43 | geometry types within a single layer. Direct attempts to translate result |
| 44 | in errors like this: |
| 45 | |
| 46 | {{{ |
| 47 | % ogr2ogr out.shp mixed.dgn |
| 48 | ERROR 1: Attempt to write non-linestring (POLYGON) geometry to ARC type shapefile. |
| 49 | ERROR 1: Terminating translation prematurely after failed |
| 50 | translation of layer elements |
| 51 | }}} |
| 52 | |
| 53 | The first step in dealing with such a problem is to discover what |
| 54 | geometry types exist in the source file. For a DGN file called |
| 55 | mixed.dgn, with a layer called elements this can be accomplished |
| 56 | using the following OGR SQL command (see |
| 57 | [http://www.gdal.org/ogr/ogr_sql.html OGR SQL] tutorial for details): |
| 58 | |
| 59 | {{{ |
| 60 | % ogrinfo -ro mixed.dgn -sql 'select distinct ogr_geometry from elements' |
| 61 | INFO: Open of `mixed.dgn' |
| 62 | using driver `DGN' successful. |
| 63 | |
| 64 | Layer name: elements |
| 65 | Geometry: Unknown (any) |
| 66 | Feature Count: 1 |
| 67 | Layer SRS WKT: |
| 68 | (unknown) |
| 69 | ogr_geometry: String (0.0) |
| 70 | OGRFeature(elements):0 |
| 71 | ogr_geometry (String) = LINESTRING |
| 72 | |
| 73 | OGRFeature(elements):1 |
| 74 | ogr_geometry (String) = POLYGON |
| 75 | |
| 76 | OGRFeature(elements):2 |
| 77 | ogr_geometry (String) = POINT |
| 78 | }}} |
| 79 | |
| 80 | This file has point, line and polygon geometries. Each will need to be |
| 81 | translated to a separate output file. |
| 82 | |
| 83 | {{{ |
| 84 | % ogr2ogr out_point.shp mixed.dgn -where 'ogr_geometry = "POINT"' |
| 85 | % ogr2ogr out_line.shp mixed.dgn -where 'ogr_geometry = "LINESTRING"' |
| 86 | % ogr2ogr out_poly.shp mixed.dgn -where 'ogr_geometry = "POLYGON"' |
| 87 | }}} |
| 88 | |
| 89 | |
| 90 | |