Index: /issm/trunk/src/c/modules/Shp2Kmlx/Shp2Kmlx.cpp
===================================================================
--- /issm/trunk/src/c/modules/Shp2Kmlx/Shp2Kmlx.cpp	(revision 10274)
+++ /issm/trunk/src/c/modules/Shp2Kmlx/Shp2Kmlx.cpp	(revision 10275)
@@ -19,5 +19,6 @@
 	double  cm,sp;
 
-	Xy2lldef(&cm,&sp,sgn);
+	if (sgn)
+		Xy2lldef(&cm,&sp,sgn);
 
 	return(Shp2Kmlx(filshp,filkml,
@@ -34,10 +35,10 @@
 	#ifdef _HAVE_SHAPELIB_ //only works if Shapelib library has been compiled in.
 	
-	int     i,j,iret=0;
+	int     i,j,k,iret=0;
 	int     lwidth=1;
 	double  popac=0.50;
-	int     nprof;
-	int     *pnvert=NULL,*pptype=NULL;
-	double  **pprofx=NULL,**pprofy=NULL,**pprofz=NULL,**pprofm=NULL;
+	int     nshap;
+	int     *pstype=NULL,*pnpart=NULL,**ppstrt=NULL,**pptype=NULL,*pnvert=NULL;
+	double  **pshapx=NULL,**pshapy=NULL,**pshapz=NULL,**pshapm=NULL;
 	bool    *closed=NULL;
 	double  *lat=NULL,*lon=NULL;
@@ -49,14 +50,16 @@
 
 	char    indent[81]="";
-	KML_File*          kfile =NULL;
-	KML_Document*      kdoc  =NULL;
-	KML_Style*         kstyle=NULL;
-	KML_LineStyle*     klsty =NULL;
-	KML_PolyStyle*     kpsty =NULL;
-	KML_Folder*        kfold =NULL;
-	KML_Placemark*     kplace=NULL;
-	KML_Polygon*       kpoly =NULL;
-	KML_LinearRing*    kring =NULL;
-	KML_LineString*    kline =NULL;
+	KML_File*             kfile =NULL;
+	KML_Document*         kdoc  =NULL;
+	KML_Style*            kstyle=NULL;
+	KML_LineStyle*        klsty =NULL;
+	KML_PolyStyle*        kpsty =NULL;
+	KML_Folder*           kfold =NULL;
+	KML_Placemark*        kplace=NULL;
+	KML_MultiGeometry*    kmulti=NULL;
+	KML_Polygon*          kpoly =NULL;
+	KML_LinearRing*       kring =NULL;
+	KML_LineString*       kline =NULL;
+	KML_Point*            kpoint=NULL;
 
 	FILE*   fid=NULL;
@@ -94,13 +97,14 @@
 			adfMaxBound[3] );
 
-	nprof=nEntities;
-	pnvert=(int *) xmalloc(nprof*sizeof(int));
-	pptype=(int *) xmalloc(nprof*sizeof(int));
-	pprofx=(double **) xmalloc(nprof*sizeof(double *));
-	pprofy=(double **) xmalloc(nprof*sizeof(double *));
-	pprofz=(double **) xmalloc(nprof*sizeof(double *));
-	pprofm=(double **) xmalloc(nprof*sizeof(double *));
-
-	if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filshp,false))
+	nshap=nEntities;
+	pstype=(int *) xmalloc(nshap*sizeof(int));
+	pnpart=(int *) xmalloc(nshap*sizeof(int));
+	ppstrt=(int **) xmalloc(nshap*sizeof(int *));
+	pptype=(int **) xmalloc(nshap*sizeof(int *));
+	pnvert=(int *) xmalloc(nshap*sizeof(int));
+	pshapx=(double **) xmalloc(nshap*sizeof(double *));
+	pshapy=(double **) xmalloc(nshap*sizeof(double *));
+	pshapz=(double **) xmalloc(nshap*sizeof(double *));
+	pshapm=(double **) xmalloc(nshap*sizeof(double *));
 
 /*  loop over the list of shapes  */
@@ -123,10 +127,27 @@
 				psShape->dfZMax, psShape->dfMMax );
 
+	pstype[i]=psShape->nSHPType;
+	pnpart[i]=psShape->nParts;
+	if (pnpart[i]) {
+		ppstrt[i]=(int *) xmalloc(pnpart[i]*sizeof(int));
+		pptype[i]=(int *) xmalloc(pnpart[i]*sizeof(int));
+	}
+	else {
+		ppstrt[i]=NULL;
+		pptype[i]=NULL;
+	}
 	pnvert[i]=psShape->nVertices;
-	pptype[i]=psShape->nSHPType;
-	pprofx[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
-	pprofy[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
-	pprofz[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
-	pprofm[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
+	if (pnvert[i]) {
+		pshapx[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
+		pshapy[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
+		pshapz[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
+		pshapm[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
+	}
+	else {
+		pshapx[i]=NULL;
+		pshapy[i]=NULL;
+		pshapz[i]=NULL;
+		pshapm[i]=NULL;
+	}
 
 	for( j = 0, iPart = 1; j < psShape->nVertices; j++ )
@@ -135,5 +156,9 @@
 
 			if( j == 0 && psShape->nParts > 0 )
+			{
 				pszPartType = SHPPartTypeName( psShape->panPartType[0] );
+				ppstrt[i][0]=psShape->panPartStart[0];
+				pptype[i][0]=psShape->panPartType[0];
+			}
 
 		if( iPart < psShape->nParts
@@ -141,4 +166,6 @@
 		{
 				pszPartType = SHPPartTypeName( psShape->panPartType[iPart] );
+				ppstrt[i][iPart]=psShape->panPartStart[iPart];
+				pptype[i][iPart]=psShape->panPartType[iPart];
 		iPart++;
 		pszPlus = "+";
@@ -155,8 +182,8 @@
 //				   pszPartType );
 
-		pprofx[i][j]=psShape->padfX[j];
-		pprofy[i][j]=psShape->padfY[j];
-		pprofz[i][j]=psShape->padfZ[j];
-		pprofm[i][j]=psShape->padfM[j];
+		pshapx[i][j]=psShape->padfX[j];
+		pshapy[i][j]=psShape->padfY[j];
+		pshapz[i][j]=psShape->padfZ[j];
+		pshapm[i][j]=psShape->padfM[j];
 	}
 
@@ -179,10 +206,9 @@
 
 	SHPClose( hSHP );
-	return(iret);
-
-/*  read shp file  */
-
-	if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filshp,false))
-		_error_("Error reading shp file.");
+
+/*  read exp file  */
+
+//	if (!DomainOutlineRead(&nshap,&pnvert,&pshapx,&pshapy,&closed,filshp,false))
+//		_error_("Error reading exp file.");
 
 /*  construct kml file  */
@@ -238,16 +264,123 @@
 	(kdoc->style     )->AddObject((Object*)kstyle);
 
-/*  construct kml folder for polygons  */
+/*  construct kml folder for shapes  */
 
 	kfold =new KML_Folder();
-	sprintf(kfold->name      ,"Profiles translated from file \"%s\".",filshp);
+	sprintf(kfold->name      ,"Shapefile: %s  Type: %s  nShapes: %d",
+			filshp, SHPTypeName( nShapeType ), nEntities );
 	kfold->open      =1;
 
+/*  loop over the list of shapes  */
+
+	for (i=0; i<nshap; i++) {
+
+/*  null type  */
+
+		if      (pstype[i] == SHPT_NULL) {
+			;
+		}
+
+/*  point types  */
+
+		else if (pstype[i] == SHPT_POINT ||
+			  	 pstype[i] == SHPT_POINTZ ||
+			 	 pstype[i] == SHPT_POINTM) {
+			kplace=new KML_Placemark();
+
+			sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
+					i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
+			kplace->visibility=true;
+			sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
+
+			if (pnpart[i] > 0)
+				_printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 0 parts.\n",
+						 i,SHPTypeName( pstype[i] ),pnpart[i]);
+			if (pnvert[i] > 1)
+				_printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 1 vertices.\n",
+						 i,SHPTypeName( pstype[i] ),pnvert[i]);
+
+			kpoint=new KML_Point();
+
+			lat=(double *) xmalloc(pnvert[i]*sizeof(double));
+			lon=(double *) xmalloc(pnvert[i]*sizeof(double));
+			if (sgn) {
+				Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
+			}
+			else  {
+				memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
+				memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
+			}
+
+			kpoint->coords[0]=lon      [0];
+			kpoint->coords[1]=lat      [0];
+			kpoint->coords[2]=pshapz[i][0];
+
+			xfree((void**)&lon);
+			xfree((void**)&lat);
+
+			(kplace->geometry  )->AddObject((Object*)kpoint);
+			kpoint=NULL;
+			(kfold ->feature   )->AddObject((Object*)kplace);
+			kplace=NULL;
+		}
+
+/*  polyline types  */
+
+		else if (pstype[i] == SHPT_ARC ||
+				 pstype[i] == SHPT_ARCZ ||
+				 pstype[i] == SHPT_ARCM) {
+			kplace=new KML_Placemark();
+
+			sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
+					i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
+			kplace->visibility=true;
+			sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
+
+			kmulti=new KML_MultiGeometry();
+
+			lat=(double *) xmalloc(pnvert[i]*sizeof(double));
+			lon=(double *) xmalloc(pnvert[i]*sizeof(double));
+			if (sgn) {
+				Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
+			}
+			else  {
+				memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
+				memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
+			}
+
+			for (j=0; j<pnpart[i]; j++) {
+				kline =new KML_LineString();
+
+				kline->ncoord    =(j<pnpart[i]-1 ? ppstrt[i][j+1]-ppstrt[i][j] : pnvert[i]-ppstrt[i][j]);
+				kline->coords    =(double (*)[3]) xmalloc(kline->ncoord*3*sizeof(double));
+				for (k=0; k<kline->ncoord; k++) {
+					kline->coords[k][0]=lon      [ppstrt[i][j]+k];
+					kline->coords[k][1]=lat      [ppstrt[i][j]+k];
+					kline->coords[k][2]=pshapz[i][ppstrt[i][j]+k];
+				}
+
+				(kmulti->geometry  )->AddObject((Object*)kline);
+				kline =NULL;
+			}
+
+			xfree((void**)&lon);
+			xfree((void**)&lat);
+
+			(kplace->geometry  )->AddObject((Object*)kmulti);
+			kmulti=NULL;
+			(kfold ->feature   )->AddObject((Object*)kplace);
+			kplace=NULL;
+		}
+
+/*  polygon types  */
+
+		else if (pstype[i] == SHPT_POLYGON ||
+				 pstype[i] == SHPT_POLYGONZ ||
+				 pstype[i] == SHPT_POLYGONM) {
+			_printf_(true,"Warning -- Shape %d of type \"%s\" will be ignored.\n",
+					 i,SHPTypeName( pstype[i] ));
+			continue;
+
 /*  polygon with multiple holes  */
-
-	if (holes && nprof && !closed[0]) {
-		_printf_(true,"Warning -- Outer profile is not closed, so \"holes\" option will be ignored.\n");
-		holes=false;
-	}
 
 	if (holes) {
@@ -263,5 +396,5 @@
 		lat=(double *) xmalloc(pnvert[i]*sizeof(double));
 		lon=(double *) xmalloc(pnvert[i]*sizeof(double));
-		Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
+		Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
 
 		kring->ncoord    =pnvert[i];
@@ -278,5 +411,5 @@
 		kring =NULL;
 
-		for (i=1; i<nprof; i++) {
+		for (i=1; i<nshap; i++) {
 			if (!closed[i]) {
 				_printf_(true,"Warning -- Inner profile %d is not closed with \"holes\" specified, so it will be ignored.\n",i+1);
@@ -288,5 +421,5 @@
 			lat=(double *) xmalloc(pnvert[i]*sizeof(double));
 			lon=(double *) xmalloc(pnvert[i]*sizeof(double));
-			Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
+			Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
 			kring->ncoord    =pnvert[i];
 			kring->coords    =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
@@ -312,5 +445,5 @@
 
 	else {
-		for (i=0; i<nprof; i++) {
+		for (i=0; i<nshap; i++) {
 			kplace=new KML_Placemark();
 
@@ -325,5 +458,5 @@
 				lat=(double *) xmalloc(pnvert[i]*sizeof(double));
 				lon=(double *) xmalloc(pnvert[i]*sizeof(double));
-				Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
+				Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
 
 				kring->ncoord    =pnvert[i];
@@ -344,32 +477,70 @@
 			}
 
-			else {
-				sprintf(kplace->name      ,"LineString %d",i+1);
-				kplace->visibility=true;
-				sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
-
-				kline =new KML_LineString();
-
-				lat=(double *) xmalloc(pnvert[i]*sizeof(double));
-				lon=(double *) xmalloc(pnvert[i]*sizeof(double));
-				Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
-
-				kline->ncoord    =pnvert[i];
-				kline->coords    =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
-				for (j=0; j<pnvert[i]; j++) {
-					kline->coords[j][0]=lon[j];
-					kline->coords[j][1]=lat[j];
-					kline->coords[j][2]=0.;
-				}
-				xfree((void**)&lon);
-				xfree((void**)&lat);
-
-				(kplace->geometry  )->AddObject((Object*)kline);
-				kline =NULL;
-			}
-
 			(kfold ->feature   )->AddObject((Object*)kplace);
 			kplace=NULL;
 		}
+	}
+		}
+
+/*  multipoint types  */
+
+		else if (pstype[i] == SHPT_MULTIPOINT ||
+				 pstype[i] == SHPT_MULTIPOINTZ ||
+				 pstype[i] == SHPT_MULTIPOINTM) {
+			kplace=new KML_Placemark();
+
+			sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
+					i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
+			kplace->visibility=true;
+			sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
+
+			if (pnpart[i] > 0)
+				_printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 0 parts.\n",
+						 i,SHPTypeName( pstype[i] ),pnpart[i]);
+
+			kmulti=new KML_MultiGeometry();
+
+			lat=(double *) xmalloc(pnvert[i]*sizeof(double));
+			lon=(double *) xmalloc(pnvert[i]*sizeof(double));
+			if (sgn) {
+				Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
+			}
+			else  {
+				memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
+				memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
+			}
+
+			for (j=0; j<pnvert[i]; j++) {
+				kpoint=new KML_Point();
+
+				kpoint->coords[0]=lon      [j];
+				kpoint->coords[1]=lat      [j];
+				kpoint->coords[2]=pshapz[i][j];
+
+				(kmulti->geometry  )->AddObject((Object*)kpoint);
+				kpoint=NULL;
+			}
+
+			xfree((void**)&lon);
+			xfree((void**)&lat);
+
+			(kplace->geometry  )->AddObject((Object*)kmulti);
+			kmulti=NULL;
+			(kfold ->feature   )->AddObject((Object*)kplace);
+			kplace=NULL;
+		}
+
+/*  multipatch types  */
+
+		else if (pstype[i] == SHPT_MULTIPATCH) {
+		}
+
+/*  unknown type  */
+
+		else {
+			_printf_(true,"Warning -- Shape %d of type \"%s\" will be ignored.\n",
+					 i,SHPTypeName( pstype[i] ));
+		}
+
 	}
 
@@ -390,9 +561,23 @@
 
 	delete kfile;
-	for (i=nprof-1; i>=0; i--) {
-		xfree((void**)&(pprofy[i]));
-		xfree((void**)&(pprofx[i]));
-	}
+	for (i=nshap-1; i>=0; i--) {
+		xfree((void**)&(pshapm[i]));
+		xfree((void**)&(pshapz[i]));
+		xfree((void**)&(pshapy[i]));
+		xfree((void**)&(pshapx[i]));
+	}
+	xfree((void**)&pshapm);
+	xfree((void**)&pshapz);
+	xfree((void**)&pshapy);
+	xfree((void**)&pshapx);
 	xfree((void**)&pnvert);
+	for (i=nshap-1; i>=0; i--) {
+		xfree((void**)&(pptype[i]));
+		xfree((void**)&(ppstrt[i]));
+	}
+	xfree((void**)&pptype);
+	xfree((void**)&ppstrt);
+	xfree((void**)&pnpart);
+	xfree((void**)&pstype);
 
 	clock1=clock();
