SpeciesFileGroup / taxonworks

Workbench for biodiversity informatics.
http://taxonworks.org
MIT License
87 stars 26 forks source link

GeographicItem geographic values randomly typed as String rather than RGeo types #1553

Closed LocoDelAssembly closed 4 years ago

LocoDelAssembly commented 4 years ago

There is a recent increase of exception notification messages (e.g. "undefined methods contains? for String", undefined method 'x' for String, etc.). I already found these errors very unexpected, but the latest ones from collecting_event/autocomplete (which cause "undefined method 'x'") are even more surprising to me.

I've tried several times to replicate locally by getting a copy of the production database and exercising the URLs but nothing happened.

Also scanned the database to see if I can get a String instead of RGeo subtype, but other than NilClass, no other types emerged. For the error caused at https://github.com/SpeciesFileGroup/taxonworks/blob/11db6e6bd/app/helpers/collecting_events_helper.rb#L70 I tested with this code:

$ time rails r "puts Parallel.map(CollectingEvent.find_each, progress: '') { |g| g.map_center.class }.uniq"
 |Time: 00:17:28 | =========== | Time: 00:17:28
RGeo::Geographic::ProjectedPointImpl
NilClass

real    17m44,504s
user    222m33,025s
sys 0m39,082s

I don't know what else to do. The referrer seems to always be Comprehensive Specimen Digitization task, and the same project and perhaps user. However, even if there is a frontend bug, I don't see how could it explain getting strings instead of geographic projections.

Also looked into RGeo, postgis-adapter and ActiveModel, in particular code around cast_value, but unless in some instances the column is generically typed instead of getting a concrete subclass, it should not return a String.

Wonder if Zeitwerk would fix something.


Ref https://github.com/SpeciesFileGroup/taxonworks/commit/ecfafd1b16797b8202c55034da4345eb8af860e5 (some of the contains? error, others happen inside RGeo gem because rhs is String)

Georeference.all.map { |g| g.error_geographic_item&.geo_object.class }.uniq

I get:

[NilClass, RGeo::Geographic::ProjectedPolygonImpl]
mjy commented 4 years ago

This is likely due to a bad string not being handled in the Georeference::GeoLocate. IIRC the param is passed in as shape, to a virtual attribute, then attempted to be converted before save. The Georeference then does a test to see that the Collecting Event geographic area contains that area- which likely isn't correct, and a string, not a GeographicItem, because of a failed parse?

LocoDelAssembly commented 4 years ago

@mjy my thinking was it could be bad params during create/update, but now we also have problems with autocomplete which works with objects from DB only. Either or I'm extremely unlucky and I always get a database dump by the time the offending GeographicItem is fixed or there is something else going on.

Also, given cast_value and parse_wkt (which actually also parses WKB), parse errors should result in nil to be returned, not raw column value.

LocoDelAssembly commented 4 years ago

@mjy discovered related issues in RGeo repos. Enumerating below to keep track: https://github.com/rgeo/activerecord-postgis-adapter/pull/309 https://github.com/rgeo/activerecord-postgis-adapter/issues/307