Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 8.2] Cache result of proj_get_type() to help for performance of proj_factors() (fixes #2965) #2967

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 89 additions & 81 deletions src/iso19111/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,98 +1150,106 @@ PJ_TYPE proj_get_type(const PJ *obj) {
if (!obj || !obj->iso_obj) {
return PJ_TYPE_UNKNOWN;
}
auto ptr = obj->iso_obj.get();
if (dynamic_cast<Ellipsoid *>(ptr)) {
return PJ_TYPE_ELLIPSOID;
}
if (obj->type != PJ_TYPE_UNKNOWN)
return obj->type;

if (dynamic_cast<PrimeMeridian *>(ptr)) {
return PJ_TYPE_PRIME_MERIDIAN;
}
const auto getType = [&obj]() {
auto ptr = obj->iso_obj.get();
if (dynamic_cast<Ellipsoid *>(ptr)) {
return PJ_TYPE_ELLIPSOID;
}

if (dynamic_cast<DynamicGeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<GeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<DynamicVerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<VerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<DatumEnsemble *>(ptr)) {
return PJ_TYPE_DATUM_ENSEMBLE;
}
if (dynamic_cast<TemporalDatum *>(ptr)) {
return PJ_TYPE_TEMPORAL_DATUM;
}
if (dynamic_cast<EngineeringDatum *>(ptr)) {
return PJ_TYPE_ENGINEERING_DATUM;
}
if (dynamic_cast<ParametricDatum *>(ptr)) {
return PJ_TYPE_PARAMETRIC_DATUM;
}
if (dynamic_cast<PrimeMeridian *>(ptr)) {
return PJ_TYPE_PRIME_MERIDIAN;
}

{
auto crs = dynamic_cast<GeographicCRS *>(ptr);
if (crs) {
if (crs->coordinateSystem()->axisList().size() == 2) {
return PJ_TYPE_GEOGRAPHIC_2D_CRS;
} else {
return PJ_TYPE_GEOGRAPHIC_3D_CRS;
if (dynamic_cast<DynamicGeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<GeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<DynamicVerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<VerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<DatumEnsemble *>(ptr)) {
return PJ_TYPE_DATUM_ENSEMBLE;
}
if (dynamic_cast<TemporalDatum *>(ptr)) {
return PJ_TYPE_TEMPORAL_DATUM;
}
if (dynamic_cast<EngineeringDatum *>(ptr)) {
return PJ_TYPE_ENGINEERING_DATUM;
}
if (dynamic_cast<ParametricDatum *>(ptr)) {
return PJ_TYPE_PARAMETRIC_DATUM;
}

{
auto crs = dynamic_cast<GeographicCRS *>(ptr);
if (crs) {
if (crs->coordinateSystem()->axisList().size() == 2) {
return PJ_TYPE_GEOGRAPHIC_2D_CRS;
} else {
return PJ_TYPE_GEOGRAPHIC_3D_CRS;
}
}
}
}

{
auto crs = dynamic_cast<GeodeticCRS *>(ptr);
if (crs) {
if (crs->isGeocentric()) {
return PJ_TYPE_GEOCENTRIC_CRS;
} else {
return PJ_TYPE_GEODETIC_CRS;
{
auto crs = dynamic_cast<GeodeticCRS *>(ptr);
if (crs) {
if (crs->isGeocentric()) {
return PJ_TYPE_GEOCENTRIC_CRS;
} else {
return PJ_TYPE_GEODETIC_CRS;
}
}
}
}

if (dynamic_cast<VerticalCRS *>(ptr)) {
return PJ_TYPE_VERTICAL_CRS;
}
if (dynamic_cast<ProjectedCRS *>(ptr)) {
return PJ_TYPE_PROJECTED_CRS;
}
if (dynamic_cast<CompoundCRS *>(ptr)) {
return PJ_TYPE_COMPOUND_CRS;
}
if (dynamic_cast<TemporalCRS *>(ptr)) {
return PJ_TYPE_TEMPORAL_CRS;
}
if (dynamic_cast<EngineeringCRS *>(ptr)) {
return PJ_TYPE_ENGINEERING_CRS;
}
if (dynamic_cast<BoundCRS *>(ptr)) {
return PJ_TYPE_BOUND_CRS;
}
if (dynamic_cast<CRS *>(ptr)) {
return PJ_TYPE_OTHER_CRS;
}
if (dynamic_cast<VerticalCRS *>(ptr)) {
return PJ_TYPE_VERTICAL_CRS;
}
if (dynamic_cast<ProjectedCRS *>(ptr)) {
return PJ_TYPE_PROJECTED_CRS;
}
if (dynamic_cast<CompoundCRS *>(ptr)) {
return PJ_TYPE_COMPOUND_CRS;
}
if (dynamic_cast<TemporalCRS *>(ptr)) {
return PJ_TYPE_TEMPORAL_CRS;
}
if (dynamic_cast<EngineeringCRS *>(ptr)) {
return PJ_TYPE_ENGINEERING_CRS;
}
if (dynamic_cast<BoundCRS *>(ptr)) {
return PJ_TYPE_BOUND_CRS;
}
if (dynamic_cast<CRS *>(ptr)) {
return PJ_TYPE_OTHER_CRS;
}

if (dynamic_cast<Conversion *>(ptr)) {
return PJ_TYPE_CONVERSION;
}
if (dynamic_cast<Transformation *>(ptr)) {
return PJ_TYPE_TRANSFORMATION;
}
if (dynamic_cast<ConcatenatedOperation *>(ptr)) {
return PJ_TYPE_CONCATENATED_OPERATION;
}
if (dynamic_cast<CoordinateOperation *>(ptr)) {
return PJ_TYPE_OTHER_COORDINATE_OPERATION;
}
if (dynamic_cast<Conversion *>(ptr)) {
return PJ_TYPE_CONVERSION;
}
if (dynamic_cast<Transformation *>(ptr)) {
return PJ_TYPE_TRANSFORMATION;
}
if (dynamic_cast<ConcatenatedOperation *>(ptr)) {
return PJ_TYPE_CONCATENATED_OPERATION;
}
if (dynamic_cast<CoordinateOperation *>(ptr)) {
return PJ_TYPE_OTHER_COORDINATE_OPERATION;
}

return PJ_TYPE_UNKNOWN;
};

return PJ_TYPE_UNKNOWN;
obj->type = getType();
return obj->type;
}

// ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/proj_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ struct PJconsts {
mutable bool gridsNeededAsked = false;
mutable std::vector<NS_PROJ::operation::GridDescription> gridsNeeded{};

// cache pj_get_type() result to help for repeated calls to proj_factors()
mutable PJ_TYPE type = PJ_TYPE_UNKNOWN;

/*************************************************************************************
proj_create_crs_to_crs() alternative coordinate operations
**************************************************************************************/
Expand Down