Skip to content

Commit

Permalink
Set Boolean subtype for OGR Output (#6803)
Browse files Browse the repository at this point in the history
* Set Boolean subtype

* Convert true and false to 1 and 0 and add msautotest

* Update mapogroutput.cpp

Co-authored-by: Even Rouault <even.rouault@spatialys.com>

* Update mapogroutput.cpp

Co-authored-by: Even Rouault <even.rouault@spatialys.com>

* Update mapogroutput.cpp

Co-authored-by: Even Rouault <even.rouault@spatialys.com>

* Update mapogroutput.cpp

Co-authored-by: Even Rouault <even.rouault@spatialys.com>

* Use new variable for comparison

* Use Boolean field types when reading OGR data sources

* Add geopackage tests

* Add test dataset

Co-authored-by: Even Rouault <even.rouault@spatialys.com>
  • Loading branch information
geographika and rouault committed Jan 26, 2023
1 parent 67123c6 commit 3328055
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 7 deletions.
12 changes: 9 additions & 3 deletions mapogr.cpp
Expand Up @@ -2714,6 +2714,8 @@ msOGRPassThroughFieldDefinitions( layerObj *layer, msOGRFileInfo *psInfo )

for(i=0; i<numitems; i++) {
OGRFieldDefnH hField = OGR_FD_GetFieldDefn( hDefn, i );
OGRFieldSubType eFieldSubType = OGR_Fld_GetSubType(hField);

char gml_width[32], gml_precision[32];
const char *gml_type = NULL;
const char *item = OGR_Fld_GetNameRef( hField );
Expand All @@ -2723,9 +2725,13 @@ msOGRPassThroughFieldDefinitions( layerObj *layer, msOGRFileInfo *psInfo )

switch( OGR_Fld_GetType( hField ) ) {
case OFTInteger:
gml_type = "Integer";
if( OGR_Fld_GetWidth( hField) > 0 )
sprintf( gml_width, "%d", OGR_Fld_GetWidth( hField) );
if (eFieldSubType == OFSTBoolean) {
gml_type = "Boolean";
} else {
gml_type = "Integer";
if( OGR_Fld_GetWidth( hField) > 0 )
sprintf( gml_width, "%d", OGR_Fld_GetWidth( hField) );
}
break;

#if GDAL_VERSION_MAJOR >= 2
Expand Down
34 changes: 31 additions & 3 deletions mapogroutput.cpp
Expand Up @@ -494,7 +494,30 @@ static int msOGRWriteShape( OGRLayerH hOGRLayer,
}
}

OGR_F_SetFieldString( hFeat, out_field++, shape->values[i] );
const char* pszValue = shape->values[i];
#if GDAL_VERSION_MAJOR <= 2 || (GDAL_VERSION_MAJOR == 3 && GDAL_VERSION_MINOR < 7)
// Boolean true and false values are handled in GDAL 3.7+
// otherwise any string values revert to false
if (strcmp(pszValue, "true") == 0) {
OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn(hLayerDefn, out_field);
OGRFieldSubType eFieldSubType = OGR_Fld_GetSubType(hFieldDefn);
if (eFieldSubType == OFSTBoolean)
{
pszValue = "1";
}
}

if (strcmp(pszValue, "false") == 0) {
OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn(hLayerDefn, out_field);
OGRFieldSubType eFieldSubType = OGR_Fld_GetSubType(hFieldDefn);
if (eFieldSubType == OFSTBoolean)
{
pszValue = "0";
}
}
#endif

OGR_F_SetFieldString( hFeat, out_field++, pszValue );
}

/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -999,6 +1022,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
const char *name;
gmlItemObj *item = item_list->items + i;
OGRFieldType eType;
OGRFieldSubType sType = OGRFieldSubType::OFSTNone;

if( !item->visible )
continue;
Expand All @@ -1024,13 +1048,17 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
eType = OFTTime;
else if( EQUAL(item->type,"DateTime") )
eType = OFTDateTime;
else if( EQUAL(item->type,"Boolean") )
else if( EQUAL(item->type,"Boolean") ){
eType = OFTInteger;
sType = OFSTBoolean;
}
else
eType = OFTString;

hFldDefn = OGR_Fld_Create( name, eType );

if(sType != OGRFieldSubType::OFSTNone) {
OGR_Fld_SetSubType(hFldDefn, sType);
}
if( item->width != 0 )
OGR_Fld_SetWidth( hFldDefn, item->width );
if( item->precision != 0 )
Expand Down
Binary file added msautotest/wxs/data/types.gpkg
Binary file not shown.
45 changes: 45 additions & 0 deletions msautotest/wxs/expected/wfs_autotypes10_geopackage.xml
@@ -0,0 +1,45 @@
Content-Type: text/xml; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<schema
targetNamespace="http://mapserver.gis.umn.edu/mapserver"
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:gml="http://www.opengis.net/gml"
elementFormDefault="qualified" version="0.1" >

<import namespace="http://www.opengis.net/gml"
schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd" />

<element name="autotypes"
type="ms:autotypesType"
substitutionGroup="gml:_Feature" />

<complexType name="autotypesType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<element name="msGeometry" type="gml:GeometryPropertyType" minOccurs="0" maxOccurs="1"/>
<element name="mychar" minOccurs="0" type="string"/>
<element name="myvarchar" minOccurs="0" type="string"/>
<element name="mytext" minOccurs="0" type="string"/>
<element name="mybool" minOccurs="0" type="boolean"/>
<element name="myint2" minOccurs="0" type="integer"/>
<element name="myint4" minOccurs="0" type="integer"/>
<element name="myint8" minOccurs="0" type="long"/>
<element name="myfloat4" minOccurs="0" type="double"/>
<element name="myfloat8" minOccurs="0" type="double"/>
<element name="mynumeric" minOccurs="0" type="double"/>
<element name="mydate" minOccurs="0" type="date"/>
<element name="mytime" minOccurs="0" type="string"/>
<element name="mytimez" minOccurs="0" type="string"/>
<element name="mytimestamp" minOccurs="0" type="dateTime"/>
<element name="mytimestampz" minOccurs="0" type="dateTime"/>
</sequence>
</extension>
</complexContent>
</complexType>

</schema>
45 changes: 45 additions & 0 deletions msautotest/wxs/expected/wfs_autotypes11_geopackage.xml
@@ -0,0 +1,45 @@
Content-Type: text/xml; subtype=gml/3.1.1; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<schema
targetNamespace="http://mapserver.gis.umn.edu/mapserver"
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:gml="http://www.opengis.net/gml"
elementFormDefault="qualified" version="0.1" >

<import namespace="http://www.opengis.net/gml"
schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd" />

<element name="autotypes"
type="ms:autotypesType"
substitutionGroup="gml:_Feature" />

<complexType name="autotypesType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<element name="msGeometry" type="gml:GeometryPropertyType" minOccurs="0" maxOccurs="1"/>
<element name="mychar" minOccurs="0" type="string"/>
<element name="myvarchar" minOccurs="0" type="string"/>
<element name="mytext" minOccurs="0" type="string"/>
<element name="mybool" minOccurs="0" type="boolean"/>
<element name="myint2" minOccurs="0" type="integer"/>
<element name="myint4" minOccurs="0" type="integer"/>
<element name="myint8" minOccurs="0" type="long"/>
<element name="myfloat4" minOccurs="0" type="double"/>
<element name="myfloat8" minOccurs="0" type="double"/>
<element name="mynumeric" minOccurs="0" type="double"/>
<element name="mydate" minOccurs="0" type="date"/>
<element name="mytime" minOccurs="0" type="string"/>
<element name="mytimez" minOccurs="0" type="string"/>
<element name="mytimestamp" minOccurs="0" type="dateTime"/>
<element name="mytimestampz" minOccurs="0" type="dateTime"/>
</sequence>
</extension>
</complexContent>
</complexType>

</schema>
44 changes: 44 additions & 0 deletions msautotest/wxs/expected/wfs_autotypes20_geopackage.xml
@@ -0,0 +1,44 @@
Content-Type: application/gml+xml; version=3.2; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<schema
targetNamespace="http://mapserver.gis.umn.edu/mapserver"
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:gml="http://www.opengis.net/gml/3.2"
elementFormDefault="qualified" version="0.1" >

<import namespace="http://www.opengis.net/gml/3.2"
schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd" />

<element name="autotypes"
type="ms:autotypesType"
substitutionGroup="gml:AbstractFeature" />

<complexType name="autotypesType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<element name="msGeometry" type="gml:GeometryPropertyType" minOccurs="0" maxOccurs="1"/>
<element name="mychar" minOccurs="0" type="string"/>
<element name="myvarchar" minOccurs="0" type="string"/>
<element name="mytext" minOccurs="0" type="string"/>
<element name="mybool" minOccurs="0" type="boolean"/>
<element name="myint2" minOccurs="0" type="integer"/>
<element name="myint4" minOccurs="0" type="integer"/>
<element name="myint8" minOccurs="0" type="long"/>
<element name="myfloat4" minOccurs="0" type="double"/>
<element name="myfloat8" minOccurs="0" type="double"/>
<element name="mynumeric" minOccurs="0" type="double"/>
<element name="mydate" minOccurs="0" type="gml:TimeInstantType"/>
<element name="mytime" minOccurs="0" type="string"/>
<element name="mytimez" minOccurs="0" type="string"/>
<element name="mytimestamp" minOccurs="0" type="gml:TimeInstantType"/>
<element name="mytimestampz" minOccurs="0" type="gml:TimeInstantType"/>
</sequence>
</extension>
</complexContent>
</complexType>

</schema>
48 changes: 48 additions & 0 deletions msautotest/wxs/expected/wfs_getfeaturetypes10_geopackage.xml
@@ -0,0 +1,48 @@
Content-Type: text/xml; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<wfs:FeatureCollection
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd
http://mapserver.gis.umn.edu/mapserver http://localhost/path/to/wfs_simple?myparam=something&amp;SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=autotypes&amp;OUTPUTFORMAT=XMLSCHEMA">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>1.000000,2.000000 1.000000,2.000000</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<!-- WARNING: FeatureId item 'id' not found in typename 'autotypes'. -->
<gml:featureMember>
<ms:autotypes>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>1.000000,2.000000 1.000000,2.000000</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<ms:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>1.000000,2.000000</gml:coordinates>
</gml:Point>
</ms:msGeometry>
<ms:mychar>abc </ms:mychar>
<ms:myvarchar>def</ms:myvarchar>
<ms:mytext>ghi</ms:mytext>
<ms:mybool>1</ms:mybool>
<ms:myint2>10</ms:myint2>
<ms:myint4>100</ms:myint4>
<ms:myint8>1000</ms:myint8>
<ms:myfloat4>1.5</ms:myfloat4>
<ms:myfloat8>2.5</ms:myfloat8>
<ms:mynumeric>3.33</ms:mynumeric>
<ms:mydate>2023-01-01</ms:mydate>
<ms:mytime>00:00:00</ms:mytime>
<ms:mytimez>00:00:00</ms:mytimez>
<ms:mytimestamp>2023-01-01T00:00:00Z</ms:mytimestamp>
<ms:mytimestampz>2023-01-01T00:00:00Z</ms:mytimestampz>
</ms:autotypes>
</gml:featureMember>
</wfs:FeatureCollection>

49 changes: 49 additions & 0 deletions msautotest/wxs/expected/wfs_getfeaturetypes11_geopackage.xml
@@ -0,0 +1,49 @@
Content-Type: text/xml; subtype=gml/3.1.1; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<wfs:FeatureCollection
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:gml="http://www.opengis.net/gml"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapserver.gis.umn.edu/mapserver http://localhost/path/to/wfs_simple?myparam=something&amp;SERVICE=WFS&amp;VERSION=1.1.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=autotypes&amp;OUTPUTFORMAT=text/xml;%20subtype=gml/3.1.1 http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
<gml:boundedBy>
<gml:Envelope srsName="EPSG:4326">
<gml:lowerCorner>2.000000 1.000000</gml:lowerCorner>
<gml:upperCorner>2.000000 1.000000</gml:upperCorner>
</gml:Envelope>
</gml:boundedBy>
<!-- WARNING: FeatureId item 'id' not found in typename 'autotypes'. -->
<gml:featureMember>
<ms:autotypes>
<gml:boundedBy>
<gml:Envelope srsName="EPSG:4326">
<gml:lowerCorner>2.000000 1.000000</gml:lowerCorner>
<gml:upperCorner>2.000000 1.000000</gml:upperCorner>
</gml:Envelope>
</gml:boundedBy>
<ms:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:pos>2.000000 1.000000</gml:pos>
</gml:Point>
</ms:msGeometry>
<ms:mychar>abc </ms:mychar>
<ms:myvarchar>def</ms:myvarchar>
<ms:mytext>ghi</ms:mytext>
<ms:mybool>1</ms:mybool>
<ms:myint2>10</ms:myint2>
<ms:myint4>100</ms:myint4>
<ms:myint8>1000</ms:myint8>
<ms:myfloat4>1.5</ms:myfloat4>
<ms:myfloat8>2.5</ms:myfloat8>
<ms:mynumeric>3.33</ms:mynumeric>
<ms:mydate>2023-01-01</ms:mydate>
<ms:mytime>00:00:00</ms:mytime>
<ms:mytimez>00:00:00</ms:mytimez>
<ms:mytimestamp>2023-01-01T00:00:00Z</ms:mytimestamp>
<ms:mytimestampz>2023-01-01T00:00:00Z</ms:mytimestampz>
</ms:autotypes>
</gml:featureMember>
</wfs:FeatureCollection>

@@ -0,0 +1,11 @@
Content-Disposition: attachment; filename=result.dat
Content-Type: application/json

{
"type": "FeatureCollection",
"numberMatched": 1,
"name": "booleans",
"features": [
{ "type": "Feature", "properties": { "id": 1, "my_bool_true": true, "my_bool_false": false }, "geometry": { "type": "Point", "coordinates": [ 1.0, 2.0 ] } }
]
}
11 changes: 11 additions & 0 deletions msautotest/wxs/expected/wfs_getfeaturetypes20_geopackage.json
@@ -0,0 +1,11 @@
Content-Disposition: attachment; filename=result.dat
Content-Type: application/json

{
"type": "FeatureCollection",
"numberMatched": 1,
"name": "autotypes",
"features": [
{ "type": "Feature", "properties": { "mychar": "abc ", "myvarchar": "def", "mytext": "ghi", "mybool": true, "myint2": 10, "myint4": 100, "myint8": 1000, "myfloat4": 1.5, "myfloat8": 2.5, "mynumeric": 3.33, "mydate": "2023-01-01", "mytime": "00:00:00", "mytimez": "00:00:00", "mytimestamp": "2023-01-01T00:00:00", "mytimestampz": "2023-01-01T00:00:00Z" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 2.0 ] } }
]
}
49 changes: 49 additions & 0 deletions msautotest/wxs/expected/wfs_getfeaturetypes20_geopackage.xml
@@ -0,0 +1,49 @@
Content-Type: text/xml; subtype="gml/3.2.1"; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<wfs:FeatureCollection
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:wfs="http://www.opengis.net/wfs/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapserver.gis.umn.edu/mapserver http://localhost/path/to/wfs_simple?myparam=something&amp;SERVICE=WFS&amp;VERSION=2.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=autotypes&amp;OUTPUTFORMAT=application%2Fgml%2Bxml%3B%20version%3D3.2 http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd http://www.opengis.net/gml/3.2 http://schemas.opengis.net/gml/3.2.1/gml.xsd"
timeStamp="" numberMatched="1" numberReturned="1">
<wfs:boundedBy>
<gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326">
<gml:lowerCorner>2.000000 1.000000</gml:lowerCorner>
<gml:upperCorner>2.000000 1.000000</gml:upperCorner>
</gml:Envelope>
</wfs:boundedBy>
<!-- WARNING: FeatureId item 'id' not found in typename 'autotypes'. -->
<wfs:member>
<ms:autotypes>
<gml:boundedBy>
<gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326">
<gml:lowerCorner>2.000000 1.000000</gml:lowerCorner>
<gml:upperCorner>2.000000 1.000000</gml:upperCorner>
</gml:Envelope>
</gml:boundedBy>
<ms:msGeometry>
<gml:Point gml:id=".1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:pos>2.000000 1.000000</gml:pos>
</gml:Point>
</ms:msGeometry>
<ms:mychar>abc </ms:mychar>
<ms:myvarchar>def</ms:myvarchar>
<ms:mytext>ghi</ms:mytext>
<ms:mybool>1</ms:mybool>
<ms:myint2>10</ms:myint2>
<ms:myint4>100</ms:myint4>
<ms:myint8>1000</ms:myint8>
<ms:myfloat4>1.5</ms:myfloat4>
<ms:myfloat8>2.5</ms:myfloat8>
<ms:mynumeric>3.33</ms:mynumeric>
<ms:mydate gml:id=".mydate"><gml:timePosition>2023-01-01</gml:timePosition></ms:mydate>
<ms:mytime>00:00:00</ms:mytime>
<ms:mytimez>00:00:00</ms:mytimez>
<ms:mytimestamp gml:id=".mytimestamp"><gml:timePosition>2023-01-01T00:00:00Z</gml:timePosition></ms:mytimestamp>
<ms:mytimestampz gml:id=".mytimestampz"><gml:timePosition>2023-01-01T00:00:00Z</gml:timePosition></ms:mytimestampz>
</ms:autotypes>
</wfs:member>
</wfs:FeatureCollection>

0 comments on commit 3328055

Please sign in to comment.