How to avoid MapServer error when remote layer returns empty response

48 views Asked by At

My MapServer contains a layer that fetches GeoJSON features from a parameterized web service. Those features are polygons generated by aggregating points, and each feature has an attribute indicating the number of points that were aggregated. I have different classes in the mapfile to display each feature in different colors depending on the number of points.

However, depending on the parameter value, the web service may return a response with no features at all and therefore no attributes, which leads to the following MapServer error:

msOGRLayerInitItemInfo(): OGR error. Invalid Field name: num_points in layer COLLECTION_POINTS

Here's the mapfile for the corresponding layer fetching GeoJSON features from a parameterized web service:

LAYER
  NAME "COLLECTION_POINTS"
  TYPE POLYGON
  CONNECTIONTYPE OGR

  VALIDATION
    "col" "^[0-9]+$"
  END

  CONNECTION "http://myservice/%col%/agg_points"
  DATA "agg_points"

  # [snip metadata and projection stuff]

  CLASS
    NAME "<= 100 pts"
    EXPRESSION ([num_points] < 101)
    STYLE
      COLOR "#D88A01"
      OUTLINECOLOR "#D88A01"
    END
  END
  CLASS
    NAME "<= 500 pts"
    EXPRESSION ([num_points] < 501)
    STYLE
      COLOR "#EA8015"
      OUTLINECOLOR "#EA8015"
    END
  END
  CLASS
    NAME "> 500 pts"
    EXPRESSION ([num_points] > 500)
    STYLE
      COLOR "#BC2110"
      OUTLINECOLOR "#BC2110"
    END
  END
END

Is there a way to avoid that MapServer error? For instance, is it possible to check if an attribute exists before using it in a class expression? Or should I better adjust my client to handle MapServer HTML error responses?

0

There are 0 answers