| 1 | = RFC 35: Delete, reorder and alter field definitions of OGR layers = |
| 2 | |
| 3 | Authors: Even Rouault[[BR]] |
| 4 | Contact: even dot rouault at mines dash paris dot org[[BR]] |
| 5 | Status: Developement |
| 6 | |
| 7 | == Summary == |
| 8 | |
| 9 | This document proposes changes in OGR to add the capability to delete fields, reorder fields |
| 10 | and alter field definitions, in OGR layer definitions. |
| 11 | |
| 12 | == Rationale == |
| 13 | |
| 14 | Currently, an OGR layer definition can only be altered to add a new field definition with OGRLayer::CreateField(). |
| 15 | |
| 16 | It is desirable to extend those capabilities to be able to delete, reorder and alter field |
| 17 | definitions of existing layers. Such wish has been expressed in ticket #2671 and comes back regularly |
| 18 | on QGIS mailing list (e.g. http://lists.osgeo.org/pipermail/qgis-user/2011-May/011935.html). |
| 19 | QGIS currently has a "Table Manager" to work around the lack of DeleteField(), so a proper solution is |
| 20 | clearly needed. |
| 21 | |
| 22 | == Planned Changes == |
| 23 | |
| 24 | The OGRLayer class will be extended with the following methods : |
| 25 | {{{ |
| 26 | virtual OGRErr DeleteField( int iField ); |
| 27 | virtual OGRErr ReorderField( int iOldFieldPos, int iNewFieldPos ); |
| 28 | virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags ); |
| 29 | }}} |
| 30 | |
| 31 | The documentation of those new methods is : |
| 32 | {{{ |
| 33 | |
| 34 | /** |
| 35 | \fn OGRErr OGRLayer::DeleteField( int iField ); |
| 36 | |
| 37 | \brief Delete an existing field on a layer. |
| 38 | |
| 39 | You must use this to delete existing fields |
| 40 | on a real layer. Internally the OGRFeatureDefn for the layer will be updated |
| 41 | to reflect the deleted field. Applications should never modify the OGRFeatureDefn |
| 42 | used by a layer directly. |
| 43 | |
| 44 | This function should not be called while there are feature objects in existance that |
| 45 | were obtained or created with the previous layer definition. |
| 46 | |
| 47 | Not all drivers support this method. You can query a layer to check if it supports it |
| 48 | with the OLCDeleteField capability. Some drivers may only support this method while |
| 49 | there are still no features in the layer. When it is supported, the existings features of the |
| 50 | backing file/database should be updated accordingly. |
| 51 | |
| 52 | This function is the same as the C function OGR_L_DeleteField(). |
| 53 | |
| 54 | @param iField index of the field to delete. |
| 55 | |
| 56 | @return OGRERR_NONE on success. |
| 57 | |
| 58 | @since OGR 1.9.0 |
| 59 | */ |
| 60 | |
| 61 | /** |
| 62 | \fn OGRErr OGRLayer::ReorderField( int iOldFieldPos, int iNewFieldPos ); |
| 63 | |
| 64 | \brief Reorder an existing field on a layer. |
| 65 | |
| 66 | You must use this to reorder existing fields |
| 67 | on a real layer. Internally the OGRFeatureDefn for the layer will be updated |
| 68 | to reflect the reordering of the fields. Applications should never modify the OGRFeatureDefn |
| 69 | used by a layer directly. |
| 70 | |
| 71 | This function should not be called while there are feature objects in existance that |
| 72 | were obtained or created with the previous layer definition. |
| 73 | |
| 74 | Not all drivers support this method. You can query a layer to check if it supports it |
| 75 | with the OLCReorderField capability. Some drivers may only support this method while |
| 76 | there are still no features in the layer. When it is supported, the existings features of the |
| 77 | backing file/database should be updated accordingly. |
| 78 | |
| 79 | This function is the same as the C function OGR_L_ReorderField(). |
| 80 | |
| 81 | @param iOldFieldPos previous position of the field to move. |
| 82 | @param iNewFieldPos new position of the field to move. |
| 83 | |
| 84 | @return OGRERR_NONE on success. |
| 85 | |
| 86 | @since OGR 1.9.0 |
| 87 | */ |
| 88 | |
| 89 | /** |
| 90 | \fn OGRErr OGRLayer::AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags ); |
| 91 | |
| 92 | \brief Alter the definition of an existing field on a layer. |
| 93 | |
| 94 | You must use this to alter the definition of an existing field of a real layer. |
| 95 | Internally the OGRFeatureDefn for the layer will be updated |
| 96 | to reflect the altered field. Applications should never modify the OGRFeatureDefn |
| 97 | used by a layer directly. |
| 98 | |
| 99 | This function should not be called while there are feature objects in existance that |
| 100 | were obtained or created with the previous layer definition. |
| 101 | |
| 102 | Not all drivers support this method. You can query a layer to check if it supports it |
| 103 | with the OLCAlterFieldDefn capability. Some drivers may only support this method while |
| 104 | there are still no features in the layer. When it is supported, the existings features of the |
| 105 | backing file/database should be updated accordingly. Some drivers might also not support |
| 106 | all update flags. |
| 107 | |
| 108 | This function is the same as the C function OGR_L_AlterFieldDefn(). |
| 109 | |
| 110 | @param iField index of the field whose definition must be altered. |
| 111 | @param poNewFieldDefn new field definition |
| 112 | @param nFlags combination of ALTER_NAME_FLAG, ALTER_TYPE_FLAG and ALTER_WIDTH_PRECISION_FLAG |
| 113 | to indicate which of the name and/or type and/or width and precision fields from the new field |
| 114 | definition must be taken into account. |
| 115 | |
| 116 | @return OGRERR_NONE on success. |
| 117 | |
| 118 | @since OGR 1.9.0 |
| 119 | */ |
| 120 | }}} |
| 121 | |
| 122 | Three new layer capabilities are added : |
| 123 | {{{ |
| 124 | OLCDeleteField / "DeleteField": TRUE if this layer can delete |
| 125 | existing fields on the current layer using DeleteField(), otherwise FALSE. |
| 126 | |
| 127 | OLCReorderField / "ReorderField": TRUE if this layer can reorder |
| 128 | existing fields on the current layer using ReorderField(), otherwise FALSE. |
| 129 | |
| 130 | OLCAlterFieldDefn / "AlterFieldDefn": TRUE if this layer can alter |
| 131 | the definition of an existing field on the current layer using AlterFieldDefn(), otherwise FALSE. |
| 132 | }}} |
| 133 | |
| 134 | |
| 135 | The new methods are mapped to the C API : |
| 136 | {{{ |
| 137 | OGRErr CPL_DLL OGR_L_DeleteField( OGRLayerH, int iField ); |
| 138 | OGRErr CPL_DLL OGR_L_ReorderField( OGRLayerH, int iOldFieldPos, int iNewFieldPos ); |
| 139 | OGRErr CPL_DLL OGR_L_AlterFieldDefn( OGRLayerH, int iField, OGRFieldDefnH hNewFieldDefn, int nFlags ); |
| 140 | }}} |
| 141 | |
| 142 | For the purpose of the implementation, new methods are also added to the OGRFeatureDefn class : |
| 143 | {{{ |
| 144 | OGRErr DeleteFieldDefn( int iField ); |
| 145 | OGRErr ReorderFieldDefn( int iOldFieldPos, int iNewFieldPos ); |
| 146 | }}} |
| 147 | |
| 148 | == Compatibility Issues == |
| 149 | |
| 150 | None |
| 151 | |
| 152 | == Changed drivers == |
| 153 | |
| 154 | The shapefile driver will implement DeleteField(), ReorderField() and AlterFieldDefn(). Shapelib will be |
| 155 | extended with DBFReorderField() and DBFAlterFieldDefn(). |
| 156 | |
| 157 | Note: The implementation of AlterFieldDefn() in the Shapefile driver does not currently support altering |
| 158 | the field type, and will not reformat numeric values of existing features if width or precision are changed. |
| 159 | However, appropriate field truncation or expansion will occur if the width is altered. |
| 160 | |
| 161 | Other drivers, mainly database drivers (PG, MySQL, SQLite), could be easily extented to implement the new API |
| 162 | by issuing the appropriate SQL command (DROP COLUMN, ALTER TABLE foo ALTER COLUMN bar, ...). |
| 163 | The implementation of DeleteField() and AlterFieldDefn() in the PG driver is indeed planned, |
| 164 | provided this RFC is adopted. |
| 165 | |
| 166 | == SWIG bindings == |
| 167 | |
| 168 | DeleteField(), ReorderField() and AlterFieldDefn() will be mapped to SWIG. |
| 169 | |
| 170 | == Test Suite == |
| 171 | |
| 172 | The autotest suite will be extended to test the implementation of the new API for the Shapefile driver. An example |
| 173 | of the use of the new API is attached to ticket #2671 (rfc35_test.py) and will be turned into unit tests. |
| 174 | |
| 175 | == Implementation == |
| 176 | |
| 177 | Implementation will be done by Even Rouault in GDAL/OGR trunk. Changes in Shapelib will need to be |
| 178 | pushed into upstream CVS by a Shapelib committer. The proposed implementation is attached as |
| 179 | a patch in ticket #2671 (rfc35.patch). |