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

Additions to netCDF Driver to allow Reading from CF-1.8 Encoded Geometries (#1287) #1541

Merged
merged 108 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
a71ed08
Some preliminary work- add defines for new special simple geometry at…
wchen329 Feb 16, 2019
e6892ee
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 1, 2019
8c056d9
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 4, 2019
6088a59
Merge branch 'master' of https://github.com/wchen329/gdal
wchen329 Apr 4, 2019
6d720d4
minor change in perm
wchen329 Apr 4, 2019
70bef01
Add some stubs for cf-1.8, add to gmake file
wchen329 Apr 4, 2019
9a6a0ec
implemented Cf attribute retrieval, untested
wchen329 Apr 4, 2019
eababe8
implemented Cf Conventions attribute retrieval, untested
wchen329 Apr 4, 2019
cebc442
Merge branch 'master' of https://github.com/wchen329/gdal
wchen329 Apr 4, 2019
6980fff
Added most of impl for getGeometryType
wchen329 Apr 4, 2019
65c6b36
more on implementation, stubs, and members of SGeometry
wchen329 Apr 4, 2019
181cf94
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 4, 2019
952d5c2
more work on SGeometry
wchen329 Apr 5, 2019
be185bb
SGeometry constructor finished
wchen329 Apr 5, 2019
0351134
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 5, 2019
3a92af1
next pt feature [strinng needs testing]
wchen329 Apr 5, 2019
013eac8
Fixed issue with CF minor version detection
wchen329 Apr 5, 2019
dfd7d70
Fixed a few leaks, also some general func fixes
wchen329 Apr 5, 2019
26f4041
Fixed non-init issue for sgeometry ncid
wchen329 Apr 5, 2019
b361484
Add multiple geometry support
wchen329 Apr 5, 2019
d6efb4f
misc. fixes, small interfa cce addt
wchen329 Apr 5, 2019
d478962
Fixed swapping part node count and node count "whoops"
wchen329 Apr 5, 2019
b87c7fb
Further support for multipart geometry
wchen329 Apr 5, 2019
8dd25bc
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 26, 2019
ed739f4
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Apr 26, 2019
bf34ff8
Some more preliminary work towards tying up simple geometry reading
wchen329 Apr 28, 2019
67d5b4c
Primitive support for open netCDF dataset with CF-1.8 geometries, no …
wchen329 Apr 28, 2019
a903621
CF 1.8 single polygon detection working
wchen329 May 9, 2019
7f5bb19
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 May 9, 2019
dc24114
Re-enabled multiple features per layer
wchen329 May 10, 2019
d3708f9
Genericized layer addition for sg
wchen329 May 10, 2019
6ddcfe1
Refactored SGeometry polygon wkb, linestrings working
wchen329 May 14, 2019
66b801a
Multipolygon implementation (mainly) done
wchen329 May 14, 2019
511e438
Fixed broken polygon detection, holeless Multipolygons now readable
wchen329 May 15, 2019
6f14178
Multiline reading support
wchen329 May 15, 2019
67df5a2
Fix multipoints
wchen329 May 15, 2019
78d0acc
Points working now
wchen329 May 15, 2019
f9f0f7f
Slight refactoring
wchen329 May 15, 2019
c8dfdaf
Fix seeking bug in multipoint
wchen329 May 15, 2019
b0537d3
Add property reader, not yet integrated
wchen329 May 16, 2019
3853e00
Added mostly working interior ring polygon support
wchen329 May 16, 2019
5ca2cbf
Refactor, dependence only on geometry containers
wchen329 May 17, 2019
bd165f0
Fix small potential initialization issue
wchen329 May 17, 2019
5b1c250
Add initialization, allow for more than one Layer
wchen329 May 17, 2019
e6ded56
Interior ringed polygons (finally) working
wchen329 May 17, 2019
d844b1c
Some basic field support...
wchen329 May 17, 2019
f25bbd6
Worked in property tracking for simple geometries
wchen329 May 17, 2019
62cde13
Add to vc makefile, help with MSVC linker error
wchen329 May 19, 2019
02c9dc9
Cleaned up compiler warnings, added exception handling for several ex…
wchen329 May 19, 2019
f6afe64
Refactor, fixed another init issue
wchen329 May 20, 2019
83b3bca
Refactor mempcpy and reallocarray out
wchen329 May 20, 2019
ce1d806
Added little oom check
wchen329 May 20, 2019
b5dd276
Further "refactoring"
wchen329 May 20, 2019
9b22c9b
Add test cases, small refactoring, SRS detection (not working)
wchen329 May 20, 2019
0686795
Refactoring mentioned that was intended for previous commit
wchen329 May 20, 2019
aab1d72
Remove erroneous SRS setting
wchen329 May 21, 2019
e54dbc9
Same as last commit
wchen329 May 21, 2019
19df30e
Address compiler warnings on other platforms
wchen329 May 21, 2019
e13774e
Optimize, property scanning, small refactor
wchen329 May 21, 2019
10433c6
Further resilience against bad input updated test case
wchen329 May 22, 2019
14c2378
Missing geometry type detection
wchen329 May 22, 2019
7188525
Moved around test data, basic tests, fixed buffer overflow (still yet…
wchen329 May 23, 2019
0fc9d6f
Forgot to uncomment something
wchen329 May 23, 2019
627cea3
Add a couple of more tests
wchen329 May 23, 2019
920c0e1
Refactor according to good feedback (not done yet) primarily "safety"…
wchen329 May 23, 2019
1e05a90
Also, address buffer overflow (still not done)
wchen329 May 23, 2019
78c4d97
oops clean up, get feature count passing, fix test
wchen329 May 23, 2019
9f59507
add forgotten test itself
wchen329 May 23, 2019
bc90572
Further elaboration on tests, tests features directly using WKT repre…
wchen329 May 23, 2019
f19652d
Change exception handling to be done by reference, rather than explic…
wchen329 May 23, 2019
ab5678e
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 May 24, 2019
cb6753a
Add flipped axes test, fixed flipped axis issue, addressed more conce…
wchen329 May 24, 2019
9bdcbd5
Implementation of reset reading for simple geometries (with test)
wchen329 May 24, 2019
4715e18
Revised bad feature test (and some exceptions)
wchen329 May 24, 2019
9845edd
Add multiple geometry container / layer test
wchen329 May 25, 2019
863df6a
Added explicit support for new "geometry dimension", added copyrights…
wchen329 May 25, 2019
649435b
Fix broken test
wchen329 May 28, 2019
a9c5dab
Fix one of the casting issues presented in review, remove comments
wchen329 May 28, 2019
6843972
Attempt to address appveyor warnings
wchen329 May 28, 2019
467d7ae
Switched from expression matching to simple scanning for CF version
wchen329 May 28, 2019
7cd06a2
Switched from manual expression matching to scanning for CF version, …
wchen329 May 28, 2019
6e32835
Merge branch 'master' of https://github.com/wchen329/gdal
wchen329 May 28, 2019
9f0f314
Addressed several casting issues
wchen329 May 28, 2019
ae3440f
Minor spacing changing
wchen329 May 28, 2019
3889e3b
Remove redundant test, address another spacing concern
wchen329 May 28, 2019
156ee2b
Enforce geometry_dimension, upgrade test-cases to include geometry_di…
wchen329 May 29, 2019
6b8c3f0
Fix one last inconsistent indent
wchen329 May 29, 2019
cffdbc5
Attempt to address issues in appveyor build
wchen329 May 29, 2019
7e29b12
Address MSVC issues- in appVeyor
wchen329 May 31, 2019
cf8f7ad
Clean up test
wchen329 May 31, 2019
7513eb5
Fix delete operator mismatch from valgrind, address CI warnings
wchen329 May 31, 2019
a8f8374
Remove tabulations...
wchen329 May 31, 2019
f274fa8
Revise instance dimension scanning, remove geometry_dimension from te…
wchen329 May 31, 2019
23ed0d6
3D feature update, one basic 3d point test, more to arrive soon...
wchen329 May 31, 2019
3ea9f1b
Fix bugs in 3d feature update, add test cases for each feature type
wchen329 Jun 1, 2019
4f141c5
Small refactors, according to CI failures- 3D axis ordering test
wchen329 Jun 1, 2019
24c790b
try to make CI a little happy, light doc update
wchen329 Jun 2, 2019
35d99fa
Another attempt to make CI happy, also non-working CF-1.8 CRS setting…
wchen329 Jun 3, 2019
6959442
Doc update
wchen329 Jun 3, 2019
e0c8bd1
Again, attempt to pass last CI build
wchen329 Jun 3, 2019
0738ced
Quick fix of misplaced explicit
wchen329 Jun 3, 2019
bbdd277
Retrigger CI due to network outage, also comment out SRS setting for …
wchen329 Jun 3, 2019
4441c01
Merge branch 'master' of https://github.com/OSGeo/gdal
wchen329 Jun 3, 2019
d09095c
\Clean comment
wchen329 Jun 4, 2019
ee93221
Small declaration change, doc rewording and correction per review
wchen329 Jun 4, 2019
f9e17b8
Fixes for test_ogrsf
wchen329 Jun 4, 2019
dda0ff3
Remove hanging space...
wchen329 Jun 4, 2019
ea0c7d2
Fix blank layer issue
wchen329 Jun 4, 2019
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
Binary file added autotest/gdrivers/netcdf-sg/Yahara_alb.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/bad_feature_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/cf1.8_states.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/line_test.nc
Binary file not shown.
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/multiline_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/multipoint_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/multipolygon_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/no_nodecoords.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/point_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/polygon_test.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/serpenski_2nd.nc
Binary file not shown.
Binary file added autotest/gdrivers/netcdf-sg/unequal_xy.nc
Binary file not shown.
2 changes: 1 addition & 1 deletion gdal/frmts/netcdf/GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

include ../../GDALmake.opt

OBJ = netcdfdataset.o netcdflayer.o netcdfwriterconfig.o gmtdataset.o
OBJ = netcdfdataset.o netcdflayer.o netcdfwriterconfig.o gmtdataset.o netcdfsg.o netcdflayersg.o

XTRA_OPT =
ifeq ($(NETCDF_HAS_NC4),yes)
Expand Down
2 changes: 1 addition & 1 deletion gdal/frmts/netcdf/makefile.vc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GDAL_ROOT = ..\..

!INCLUDE $(GDAL_ROOT)\nmake.opt

OBJ = netcdfdataset.obj netcdflayer.obj netcdfwriterconfig.obj gmtdataset.obj
OBJ = netcdfdataset.obj netcdflayer.obj netcdfwriterconfig.obj gmtdataset.obj netcdfsg.obj netcdflayersg.obj

PLUGIN_DLL = gdal_netCDF.dll

Expand Down
21 changes: 19 additions & 2 deletions gdal/frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
// Must be included after standard includes, otherwise VS2015 fails when
// including <ctime>
#include "netcdfdataset.h"
#include "netcdfsg.h"
#include "netcdfuffd.h"

#include "cpl_conv.h"
Expand Down Expand Up @@ -2103,9 +2104,10 @@ netCDFDataset::netCDFDataset() :
eFormat(NCDF_FORMAT_NONE),
bIsGdalFile(false),
bIsGdalCfFile(false),

pszCFProjection(nullptr),
pszCFCoordinates(nullptr),
nCFMinorVersion(6),
bSGSupport(false),
eMultipleLayerBehaviour(SINGLE_LAYER),

// projection/GT.
Expand Down Expand Up @@ -2134,6 +2136,7 @@ netCDFDataset::netCDFDataset() :
bSignedData(true),
nLayers(0),
papoLayers(nullptr)

{
// Projection/GT.
adfGeoTransform[0] = 0.0;
Expand Down Expand Up @@ -7127,6 +7130,19 @@ GDALDataset *netCDFDataset::Open( GDALOpenInfo *poOpenInfo )
}
}

// Figure out whether or not the listed dataset has support for simple geometries (CF-1.8)
poDS->nCFMinorVersion = nccfdriver::getCFMinorVersion(cdfid);
if(poDS->nCFMinorVersion >= 8)
{
poDS->bSGSupport = true;
}
else poDS->bSGSupport = false;

if(poDS->bSGSupport)
{
poDS->DetectAndFillSGLayers(cdfid);
}

char szConventions[NC_MAX_NAME + 1];
szConventions[0] = '\0';
nc_type nAttype = NC_NAT;
Expand Down Expand Up @@ -10916,6 +10932,7 @@ CPLErr netCDFDataset::FilterVars( int nCdfId, bool bKeepRasters,
(EQUAL(osFeatureType, "profile") &&
oMapDimIdToCount.size() == 2 && nProfileDimId >= 0)) )
{
if(!bSGSupport)
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
CPLError(CE_Warning, CPLE_AppDefined,
"The dataset has several variables that could be "
"identified as vector fields, but not all share the same "
Expand Down Expand Up @@ -10954,7 +10971,7 @@ CPLErr netCDFDataset::CreateGrpVectorLayers( int nCdfId,
int nVarXId, int nVarYId, int nVarZId,
int nProfileDimId,
int nParentIndexVarID,
bool bKeepRasters )
bool bKeepRasters )
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
{
char *pszGroupName = nullptr;
NCDFGetGroupFullName(nCdfId, &pszGroupName);
Expand Down
30 changes: 29 additions & 1 deletion gdal/frmts/netcdf/netcdfdataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "gdal_pam.h"
#include "gdal_priv.h"
#include "netcdf.h"
#include "netcdfsg.h"
#include "ogr_spatialref.h"
#include "ogrsf_frmts.h"
#include "netcdfuffd.h"
Expand Down Expand Up @@ -84,6 +85,7 @@

/* NETCDF driver defs */
static const size_t NCDF_MAX_STR_LEN = 8192;
#define NCDF_CONVENTIONS "Conventions"
#define NCDF_CONVENTIONS_CF_V1_5 "CF-1.5"
#define NCDF_CONVENTIONS_CF_V1_6 "CF-1.6"
#define NCDF_SPATIAL_REF "spatial_ref"
Expand Down Expand Up @@ -250,6 +252,18 @@ static const int NCDF_DEFLATE_LEVEL = 1; /* best time/size ratio */
#define CF_PP_GRID_NORTH_POLE_LATITUDE "grid_north_pole_latitude"
#define CF_PP_NORTH_POLE_GRID_LONGITUDE "north_pole_grid_longitude"

/* Simple Geometries Special Names from CF-1.8 Draft - Chapter 7 section Geometries */
#define CF_SG_GEOMETRY "geometry"
#define CF_SG_GEOMETRY_TYPE "geometry_type"
#define CF_SG_INTERIOR_RING "interior_ring"
#define CF_SG_NODES "nodes"
#define CF_SG_NODE_COORDINATES "node_coordinates"
#define CF_SG_NODE_COUNT "node_count"
#define CF_SG_PART_NODE_COUNT "part_node_count"
#define CF_SG_TYPE_LINE "line"
#define CF_SG_TYPE_POINT "point"
#define CF_SG_TYPE_POLY "polygon"

/* -------------------------------------------------------------------- */
/* CF-1 Coordinate Type Naming (Chapter 4. Coordinate Types ) */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -808,6 +822,8 @@ class netCDFDataset final: public GDALPamDataset
bool bIsGdalCfFile; /* was this file created by the (new) CF-compliant driver? */
char *pszCFProjection;
const char *pszCFCoordinates;
int nCFMinorVersion;
bool bSGSupport;
MultipleLayerBehaviour eMultipleLayerBehaviour;
std::vector<netCDFDataset*> apoVectorDatasets;

Expand Down Expand Up @@ -889,6 +905,10 @@ class netCDFDataset final: public GDALPamDataset
int nVarXId, int nVarYId, int nVarZId,
int nProfileDimId, int nParentIndexVarID,
bool bKeepRasters );

CPLErr DetectAndFillSGLayers( int ncid );
CPLErr LoadSGVarIntoLayer( int ncid, int nc_basevarId );

protected:

CPLXMLNode *SerializeToXML( const char *pszVRTPath ) override;
Expand Down Expand Up @@ -944,6 +964,7 @@ class netCDFDataset final: public GDALPamDataset

class netCDFLayer final: public OGRLayer
{
friend class netCDFDataset;
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
typedef union
{
signed char chVal;
Expand Down Expand Up @@ -994,6 +1015,10 @@ class netCDFLayer final: public OGRLayer
nc_type m_nWKTNCDFType;
CPLString m_osCoordinatesValue;
std::vector<FieldDesc> m_aoFieldDesc;
std::vector<OGRFeature*> m_sgFeatureList;
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
std::vector<OGRFeature*>::iterator m_sgFeatItr;
bool m_sgItrInit;
bool m_HasCFSG1_8;
int m_nCurFeatureId;
CPLString m_osGridMapping;
bool m_bWriteGDALTags;
Expand All @@ -1019,9 +1044,10 @@ class netCDFLayer final: public OGRLayer
void GetNoDataValueForFloat( int nVarId, NCDFNoDataUnion* puNoData );
void GetNoDataValueForDouble( int nVarId, NCDFNoDataUnion* puNoData );
void GetNoDataValue( int nVarId, nc_type nVarType, NCDFNoDataUnion* puNoData );
bool FillFeatureFromVar(OGRFeature* poFeature, int nMainDimId, size_t nIndex);
bool FillVarFromFeature(OGRFeature* poFeature, int nMainDimId, size_t nIndex);

protected:
bool FillFeatureFromVar(OGRFeature* poFeature, int nMainDimId, size_t nIndex);
public:
netCDFLayer(netCDFDataset* poDS,
int nLayerCDFId,
Expand All @@ -1036,6 +1062,8 @@ class netCDFLayer final: public OGRLayer
void SetWKTGeometryField(const char* pszWKTVarName);
void SetGridMapping(const char* pszGridMapping);
void SetProfile(int nProfileDimID, int nParentIndexVarID);
void AddSimpleGeometryFeature(OGRFeature * sg) { this->m_sgFeatureList.push_back(sg); }
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
void EnableSGBypass() { this-> m_HasCFSG1_8 = true; }
bool AddField(int nVarId);

int GetCDFID() const { return m_nLayerCDFId; }
Expand Down
27 changes: 25 additions & 2 deletions gdal/frmts/netcdf/netcdflayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
****************************************************************************/

#include "netcdfdataset.h"
#include "netcdfsg.h"
#include "cpl_time.h"

CPL_CVSID("$Id$")
Expand Down Expand Up @@ -59,6 +60,8 @@ netCDFLayer::netCDFLayer(netCDFDataset *poDS,
m_nWKTMaxWidthDimId(-1),
m_nWKTVarID(-1),
m_nWKTNCDFType(NC_NAT),
m_sgItrInit(false),
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
m_HasCFSG1_8(false),
m_nCurFeatureId(1),
m_bWriteGDALTags(true),
m_bUseStringInNC4(true),
Expand All @@ -83,7 +86,10 @@ netCDFLayer::netCDFLayer(netCDFDataset *poDS,
/* ~netCDFLayer() */
/************************************************************************/

netCDFLayer::~netCDFLayer() { m_poFeatureDefn->Release(); }
netCDFLayer::~netCDFLayer()
{
m_poFeatureDefn->Release();
}

/************************************************************************/
/* netCDFWriteAttributesFromConf() */
Expand Down Expand Up @@ -163,6 +169,7 @@ bool netCDFLayer::Create(char **papszOptions,
"geometry type.");
}
}

else if( EQUAL(pszFeatureType, "PROFILE") )
{
if( wkbFlatten(m_poFeatureDefn->GetGeomType()) != wkbPoint )
Expand Down Expand Up @@ -1062,6 +1069,10 @@ bool netCDFLayer::FillFeatureFromVar(OGRFeature *poFeature, int nMainDimId,
}
}

// For CF-1.8 simple geometry specifically
// Only need fields to be set here
if( this-> m_HasCFSG1_8 ) return true;
wchen329 marked this conversation as resolved.
Show resolved Hide resolved

if( m_nXVarID >= 0 && m_nYVarID >= 0 &&
(m_osProfileDimName.empty() || nMainDimId == m_nProfileDimID) )
{
Expand Down Expand Up @@ -1173,8 +1184,20 @@ bool netCDFLayer::FillFeatureFromVar(OGRFeature *poFeature, int nMainDimId,

OGRFeature *netCDFLayer::GetNextFeature()
{
if(!this->m_sgItrInit && m_sgFeatureList.size() > 0)
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
{
this->m_sgFeatItr = m_sgFeatureList.begin();
this->m_sgItrInit = true;
}

if(this->m_sgItrInit && this->m_sgFeatItr != m_sgFeatureList.end())
{
return *(m_sgFeatItr++);
}

while( true )
{

OGRFeature *poFeature = GetNextRawFeature();
if( poFeature == nullptr )
return nullptr;
Expand Down Expand Up @@ -2059,7 +2082,7 @@ OGRErr netCDFLayer::CreateField(OGRFieldDefn *poFieldDefn, int /* bApproxOK */)
osVarName = CPLSPrintf("%s%d", poFieldDefn->GetNameRef(), i);
status = nc_inq_varid(m_nLayerCDFId, osVarName, &nVarID);
if( status != NC_NOERR )
break;
break;
wchen329 marked this conversation as resolved.
Show resolved Hide resolved
}
CPLDebug("netCDF", "Field %s is written in variable %s",
poFieldDefn->GetNameRef(), osVarName.c_str());
Expand Down
Loading