Skip to content

Commit 4af7028

Browse files
committed
OGR-output: output fid if requested (#5207)
* outputformat must contain option "USE_FEATUREID=true" * layer must have a ows_featureid metadata set
1 parent 6ae2bc6 commit 4af7028

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

mapogroutput.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void msOGRSetPoints( OGRGeometryH hGeom, lineObj *line, int bWant2DOutput
247247

248248
static int msOGRWriteShape( layerObj *map_layer, OGRLayerH hOGRLayer,
249249
shapeObj *shape, gmlItemListObj *item_list,
250-
int nFirstOGRFieldIndex )
250+
int nFirstOGRFieldIndex, const char *pszFeatureid )
251251

252252
{
253253
OGRGeometryH hGeom = NULL;
@@ -489,6 +489,15 @@ static int msOGRWriteShape( layerObj *map_layer, OGRLayerH hOGRLayer,
489489
for( i = 0; i < item_list->numitems; i++ ) {
490490
gmlItemObj *item = item_list->items + i;
491491

492+
if(pszFeatureid && !strcmp(pszFeatureid, item->name)) {
493+
char *endptr;
494+
long feature_id = strtol(shape->values[i],&endptr,10);
495+
if(endptr && *endptr==0) {
496+
/* only set the featureid if it is numeric */
497+
OGR_F_SetFID(hFeat, feature_id);
498+
}
499+
}
500+
492501
if( !item->visible )
493502
continue;
494503

@@ -689,6 +698,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
689698
char **file_list = NULL;
690699
int iLayer, i;
691700
int bDataSourceNameIsRequestDir = FALSE;
701+
int bUseFeatureId = MS_FALSE;
692702

693703
/* -------------------------------------------------------------------- */
694704
/* Fetch the output format driver. */
@@ -713,6 +723,9 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
713723
ds_options = CSLAddString( ds_options,
714724
format->formatoptions[i] + 5 );
715725
}
726+
if(!strcasecmp("true",msGetOutputFormatOption(format,"USE_FEATUREID","false"))) {
727+
bUseFeatureId = MS_TRUE;
728+
}
716729

717730
/* ==================================================================== */
718731
/* Determine the output datasource name to use. */
@@ -872,6 +885,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
872885
char *pszWKT;
873886
int reproject = MS_FALSE;
874887
int nFirstOGRFieldIndex = -1;
888+
const char *pszFeatureid = NULL;
875889

876890
if( !layer->resultcache )
877891
continue;
@@ -902,6 +916,9 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
902916
value = "Geometry";
903917
}
904918

919+
if(bUseFeatureId)
920+
pszFeatureid = msOWSLookupMetadata(&(layer->metadata), "FOG", "featureid");
921+
905922
if( strcasecmp(value,"Point") == 0 )
906923
eGeomType = wkbPoint;
907924
else if( strcasecmp(value,"LineString") == 0 )
@@ -1027,6 +1044,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
10271044

10281045
OGR_DS_Destroy( hDS );
10291046
msOGRCleanupDS( datasource_name );
1047+
msGMLFreeItems(item_list);
10301048
return MS_FAILURE;
10311049
}
10321050

@@ -1113,7 +1131,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
11131131

11141132
if( status == MS_SUCCESS )
11151133
status = msOGRWriteShape( layer, hOGRLayer, &resultshape,
1116-
item_list, nFirstOGRFieldIndex );
1134+
item_list, nFirstOGRFieldIndex, pszFeatureid );
11171135

11181136
if(status != MS_SUCCESS) {
11191137
OGR_DS_Destroy( hDS );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Content-Type: application/json; subtype=geojson; charset=utf-8
2+
3+
{
4+
"type": "FeatureCollection",
5+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3978" } },
6+
"features": [
7+
{ "type": "Feature", "id": 0, "properties": { "UNIQUE_KEY": "BACMK", "NAME": "\"Tignish", "CAPITAL": 0, "POP_CLASS": 1 }, "geometry": { "type": "Point", "coordinates": [ 2281604.25, 340848.9375 ] } },
8+
{ "type": "Feature", "id": 1215011, "properties": { "UNIQUE_KEY": "CAGYX", "NAME": "'Cheticamp", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2503805.5, 427872.65625 ] } },
9+
{ "type": "Feature", "id": 1209036, "properties": { "UNIQUE_KEY": "CBIKA", "NAME": "Sheet Harbour", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2499190.0, 194088.23438 ] } },
10+
{ "type": "Feature", "id": 1101036, "properties": { "UNIQUE_KEY": "BACII", "NAME": "'Souris'", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2434957.0, 347013.59375 ] } },
11+
{ "type": "Feature", "id": 1213006, "properties": { "UNIQUE_KEY": "CAGBW", "NAME": "\"Canso\"", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2578914.25, 292073.09375 ] } },
12+
{ "type": "Feature", "id": 1215002, "properties": { "UNIQUE_KEY": "CBELL", "NAME": "Port Hawkesbury", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2536072.75, 311524.96875 ] } },
13+
{ "type": "Feature", "id": 1214002, "properties": { "UNIQUE_KEY": "CAATB", "NAME": "Antigonish", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2494520.0, 285855.40625 ] } },
14+
{ "type": "Feature", "id": 1211008, "properties": { "UNIQUE_KEY": "CBKDH", "NAME": "Springhill", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2348711.75, 204294.0625 ] } },
15+
{ "type": "Feature", "id": 1208002, "properties": { "UNIQUE_KEY": "CBPAK", "NAME": "\"Wind\"sor", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2377598.0, 135734.70312 ] } },
16+
{ "type": "Feature", "id": 1206006, "properties": { "UNIQUE_KEY": "CAWAZ", "NAME": "Lunenburg", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2398677.25, 70608.14062 ] } },
17+
{ "type": "Feature", "id": 1204006, "properties": { "UNIQUE_KEY": "CAUWZ", "NAME": "Liverpool", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2388340.0, 19784.32227 ] } },
18+
{ "type": "Feature", "id": 1205016, "properties": { "UNIQUE_KEY": "CAZHC", "NAME": "Middleton", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2313364.75, 98278.92188 ] } },
19+
{ "type": "Feature", "id": 1203006, "properties": { "UNIQUE_KEY": "CAJOA", "NAME": "Digby", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2279399.0, 39858.0 ] } },
20+
{ "type": "Feature", "id": 1201008, "properties": { "UNIQUE_KEY": "CBIKP", "NAME": "Shelburne", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2359906.5, -30454.43555 ] } },
21+
{ "type": "Feature", "id": 1103025, "properties": { "UNIQUE_KEY": "BADSZ", "NAME": "Summerside", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2325561.75, 294362.25 ] } },
22+
{ "type": "Feature", "id": 1217009, "properties": { "UNIQUE_KEY": "CBQFA", "NAME": "Glace Bay", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2600377.0, 419421.71875 ] } },
23+
{ "type": "Feature", "id": 1212014, "properties": { "UNIQUE_KEY": "CBBJR", "NAME": "New Glasgow", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2449341.5, 253906.09375 ] } },
24+
{ "type": "Feature", "id": 1210006, "properties": { "UNIQUE_KEY": "CBMKT", "NAME": "Tr\"uro", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2418051.5, 205002.20312 ] } },
25+
{ "type": "Feature", "id": 1211011, "properties": { "UNIQUE_KEY": "CAAOO", "NAME": "Amherst", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2329295.75, 218397.28125 ] } },
26+
{ "type": "Feature", "id": 1207012, "properties": { "UNIQUE_KEY": "CASWE", "NAME": "Kentville", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2345781.25, 130999.54688 ] } },
27+
{ "type": "Feature", "id": 1206004, "properties": { "UNIQUE_KEY": "CAFBR", "NAME": "Bridgewater", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2382666.75, 60910.9375 ] } },
28+
{ "type": "Feature", "id": 1202006, "properties": { "UNIQUE_KEY": "CBPIB", "NAME": "Yarmouth", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2294923.75, -55344.50391 ] } },
29+
{ "type": "Feature", "id": 1217019, "properties": { "UNIQUE_KEY": "CBLHE", "NAME": "Sydney Mines", "CAPITAL": 0, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2574240.0, 416102.34375 ] } },
30+
{ "type": "Feature", "id": 1217014, "properties": { "UNIQUE_KEY": "CBLGX", "NAME": "Sydney", "CAPITAL": 0, "POP_CLASS": 4 }, "geometry": { "type": "Point", "coordinates": [ 2587598.0, 404010.53125 ] } },
31+
{ "type": "Feature", "id": 1209022, "properties": { "UNIQUE_KEY": "CAIYJ", "NAME": "Dartmouth", "CAPITAL": 0, "POP_CLASS": 5 }, "geometry": { "type": "Point", "coordinates": [ 2437026.0, 130358.35156 ] } },
32+
{ "type": "Feature", "id": 2401025, "properties": { "UNIQUE_KEY": "EGIIG", "NAME": "Fatima", "CAPITAL": 0, "POP_CLASS": 2 }, "geometry": { "type": "Point", "coordinates": [ 2402416.5, 461586.8125 ] } },
33+
{ "type": "Feature", "id": 1102075, "properties": { "UNIQUE_KEY": "BAARG", "NAME": "Charlottetown", "CAPITAL": 2, "POP_CLASS": 3 }, "geometry": { "type": "Point", "coordinates": [ 2378567.25, 305099.78125 ] } },
34+
{ "type": "Feature", "id": 1209021, "properties": { "UNIQUE_KEY": "CAPHL", "NAME": "Halifax", "CAPITAL": 2, "POP_CLASS": 6 }, "geometry": { "type": "Point", "coordinates": [ 2431943.75, 118985.38281 ] } }
35+
]
36+
}

msautotest/wxs/wfs_ogr_geojson.map

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#
2+
# Test WFS
3+
#
4+
# REQUIRES: SUPPORTS=WFS INPUT=OGR
5+
#
6+
# Test OGR geojson output.
7+
# RUN_PARMS: wfsogr10_geojson.json [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=popplace_fid&OUTPUTFORMAT=geojson" > [RESULT]
8+
MAP
9+
10+
NAME WFS_OGROUT_TEST
11+
STATUS ON
12+
SIZE 400 300
13+
EXTENT -67.5725 42 -58.9275 48.5
14+
UNITS METERS
15+
IMAGECOLOR 255 255 255
16+
SHAPEPATH ./data
17+
18+
OUTPUTFORMAT
19+
NAME "geojson"
20+
DRIVER "OGR/GEOJSON"
21+
MIMETYPE "application/json; subtype=geojson; charset=utf-8"
22+
FORMATOPTION "STORAGE=stream"
23+
FORMATOPTION "FORM=SIMPLE"
24+
FORMATOPTION "USE_FEATUREID=true"
25+
FORMATOPTION "LCO:COORDINATE_PRECISION=5"
26+
END
27+
#
28+
WEB
29+
30+
IMAGEPATH "tmp/"
31+
IMAGEURL "/ms_tmp/"
32+
33+
METADATA
34+
"ows_updatesequence" "123"
35+
"wfs_title" "Test simple wfs"
36+
"wfs_onlineresource" "http://localhost/path/to/wfs_simple?"
37+
# "wfs_srs" "EPSG:4326 EPSG:4269 EPSG:3978 EPSG:27700"
38+
"wms_title" "Test simple wms"
39+
"wms_onlineresource" "http://localhost/path/to/wms_simple?"
40+
"wms_srs" "EPSG:4269 EPSG:4326 EPSG:3978"
41+
"ows_abstract" "Test WFS Abstract"
42+
"ows_keywordlist" "ogc,wfs,gml,om"
43+
"ows_service_onlineresource" "http://localhost"
44+
"ows_fees" "none"
45+
"ows_accessconstraints" "none"
46+
"ows_addresstype" "postal"
47+
"ows_address" "123 SomeRoad Road"
48+
"ows_city" "Toronto"
49+
"ows_stateorprovince" "Ontario"
50+
"ows_postcode" "xxx-xxx"
51+
"ows_country" "Canada"
52+
"ows_contactelectronicmailaddress" "tomkralidis@xxxxxxx.xxx"
53+
"ows_contactvoicetelephone" "+xx-xxx-xxx-xxxx"
54+
"ows_contactfacsimiletelephone" "+xx-xxx-xxx-xxxx"
55+
"ows_contactperson" "Tom Kralidis"
56+
"ows_contactorganization" "MapServer"
57+
"ows_contactposition" "self"
58+
"ows_hoursofservice" "0800h - 1600h EST"
59+
"ows_contactinstructions" "during hours of service"
60+
"ows_role" "staff"
61+
"wms_feature_info_mime_type" "text/csv"
62+
"ows_enable_request" "*"
63+
END
64+
END
65+
66+
PROJECTION
67+
"+proj=latlong +datum=WGS84"
68+
END
69+
70+
71+
LAYER
72+
NAME popplace_fid
73+
CONNECTIONTYPE OGR
74+
CONNECTION "data/popplace.shp"
75+
DATA "popplace"
76+
77+
METADATA
78+
"wfs_title" "popplace"
79+
"wfs_description" "populated places"
80+
"wfs_featureid" "SGC_CODE"
81+
"wfs_getfeature_formatlist" "geojson"
82+
"gml_include_items" "NAME,UNIQUE_KEY,CAPITAL,POP_RANGE"
83+
"gml_POP_RANGE_alias" "POP_CLASS"
84+
"gml_types" "auto"
85+
END
86+
TYPE POINT
87+
STATUS ON
88+
PROJECTION
89+
"init=epsg:3978"
90+
END
91+
END # Layer
92+
93+
END # Map File

print-test-results.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ for testcase in "${tests[@]}"; do
4040
echo "# $testcase => $failedtest"
4141
echo "######################################"
4242
#for txt, gml and xml files, print a diff
43-
if echo "$failedtest" | egrep -q "(txt|xml|gml|html)$"; then
43+
if echo "$failedtest" | egrep -q "(txt|xml|gml|html|json)$"; then
4444
$DIFF -u "../expected/$failedtest" "$failedtest"
4545
fi
4646
if echo "$failedtest" | egrep -q "(png|gif|tif)$"; then

0 commit comments

Comments
 (0)