diff --git a/src/gdal.cpp b/src/gdal.cpp index 0a4818520..2a6908164 100644 --- a/src/gdal.cpp +++ b/src/gdal.cpp @@ -122,7 +122,9 @@ bool crs_is_na(Rcpp::List crs) { OGRSpatialReference *OGRSrs_from_crs(Rcpp::List crs) { OGRSpatialReference *dest = NULL; Rcpp::CharacterVector p4s = crs[1]; - Rcpp::CharacterVector wkt2 = crs[2]; + Rcpp::CharacterVector wkt2 = Rcpp::CharacterVector::create(NA_STRING); + if (crs.size() > 2) + wkt2 = crs[2]; if (!(Rcpp::CharacterVector::is_na(p4s[0]) && Rcpp::CharacterVector::is_na(wkt2[0]))) { dest = new OGRSpatialReference; dest = handle_axis_order(dest); @@ -277,13 +279,18 @@ Rcpp::List create_crs(OGRSpatialReference *ref) { crs(2) = Rcpp::CharacterVector::create(NA_STRING); } else { const char *cp; + bool epsg_is_na = true; if (ref->AutoIdentifyEPSG() == OGRERR_NONE && - (cp = ref->GetAuthorityCode(NULL)) != NULL) + (cp = ref->GetAuthorityCode(NULL)) != NULL) { crs(0) = atoi(cp); - else + epsg_is_na = false; + } else crs(0) = NA_INTEGER; crs(1) = p4s_from_spatial_reference(ref); - crs(2) = CPL_wkt2_from_epsg(crs(0)); + if (epsg_is_na) + crs(2) = Rcpp::CharacterVector::create(NA_STRING); + else + crs(2) = CPL_wkt2_from_epsg(crs(0)); } Rcpp::CharacterVector nms(3); nms(0) = "epsg"; @@ -458,8 +465,10 @@ Rcpp::List CPL_wrap_dateline(Rcpp::List sfc, Rcpp::CharacterVector opt, bool qui Rcpp::List CPL_crs_from_proj4string(Rcpp::CharacterVector p4s) { OGRSpatialReference ref; handle_axis_order(&ref); - if (ref.importFromProj4(p4s[0]) == OGRERR_NONE) + if (ref.importFromProj4((const char *) p4s[0]) == OGRERR_NONE) { + // Rcpp::Rcout << "got here" << std::endl; return create_crs(&ref); + } else { const char *cp = p4s[0]; Rf_warning("GDAL cannot import PROJ.4 string `%s': returning missing CRS\n", cp);