geographika / mappyfile

A pure Python MapFile parser for working with MapServer
MIT License
71 stars 20 forks source link

Data statements that refer to mixed case attribute names fail to parse correctly. #184

Closed ianturton closed 1 year ago

ianturton commented 1 year ago

Given a map file like:

MAP                                                                                                             
      LAYER                                                                                                         
        NAME "PlanApp_2000_2009"                                                                                            
        STATUS OFF                                                                                                          
        TYPE POLYGON                                                                                                        
        OPACITY 20                                                                                                          
        DATA "wkb_geometry from (select * FROM PLANNING_SCHEMA.allplanning_2000_onwards_polys_and_data where \"CaseFullRef\" < '09/P/99999') as foo using unique ogc_fid using   srid=27700"
        TOLERANCEUNITS PIXELS                                                                                               
        METADATA                                                                                                            
            ows_title "PlanApp_2000_2009"                                                                                     
            ows_abstract "PlanApp_2000_2009"                                                                                  
          END                                                                                                           
       VALIDATION                                                                                                          
          qstring '.'                                                                                                                                                                                                                        
        END                                                                                                                 
      CLASS                                                                                                               
        NAME ""                                                                                                           
        STYLE                                                                                                             
          COLOR 249 014 255                                                                                               
        END                                                                                                               
      END                                                                                                                 
    END                                                                                                                   
END

The Data element of the dictionary returned is 'wkb_geometry from (select * FROM PLANNING_SCHEMA.allplanning_2000_onwards_polys_and_data where ' with another element with key casefullref and value < '09/P/99999') as foo using unique ogc_fid using srid=27700

While I'd like to beat my users over the head for using mixed case attribute names in PostGIS, I can't actually convince them to change them.

geographika commented 1 year ago

That's a nasty one. The escape characters are removed as soon as they become strings in Python, so by the time they reach the parser they are seen as two separate string tokens.

>>> s = "where \"CaseFullRef\" < '09/P/99999')"
>>> print(s)
where "CaseFullRef" < '09/P/99999')

Just to check - do the field names have to be wrapped in double quotes to work in MapServer? There was a discussion at https://github.com/MapServer/MapServer/issues/3226 and code at https://github.com/MapServer/MapServer/blob/5dbbf8a19abb2f30b4852b72c8661faac14ae4c9/mappostgis.cpp#L1577 that looks like the field names could be automatically wrapped in quotes?

Possibly just wishful thinking though..

geographika commented 1 year ago

@ianturton - actually this should be supported, see the test at https://github.com/geographika/mappyfile/blob/15748919b921e1df65bcf812033c40c515568a3f/tests/test_snippets.py#L733

How are you calling mappyfile? You need to pass in a raw string for it to work correctly. I might need to add this to the high-level functions if this is not working correctly.

>>> s = r"where \"CaseFullRef\" < '09/P/99999')"
>>> print(s)
where \"CaseFullRef\" < '09/P/99999')
ianturton commented 1 year ago

That does fix it, thanks.