Merge more than two Shapefile in QGIS. Do you want to merge two or more than two Shapefile? Do all the Shapefile are of same shape type i.e the Shapefile which can be merged with each other should have an identical shape, i.e either “Point”, “Line” or “Polygon”? Do Shapefile all 3 files i.e .shx, .shp and .dbf are available? Then let us move to QGIS (Quantum GIS), an Open source Geographic information SystemSoftware. If not installed in your system, one of the way to install QGIS, with OSGeo4W provides or either you can install QGIS with standalone software. Let us move to Merge more than two Shapefile:
1.) Take and copy all Shapefiles to a common folder which you want to merge.
2.) Open up QGIS desktop and select the following from the menu :
Vector -> Data Management tools -> merge shapefile to one, which will open up new window “Merge Shapefiles”, as shown below.
3.) Now select the browse input directory to the folder you created which include all Shapefile that you need to copy. Also browse the Output directory for getting the new merged Shapefile, and name the output file, as shown below:
4.) Press Ok. It will process your all shapefile and merge it to one. Finished.
Note: New .dbf file will contain sum of column of two shapfefile minus common column.
Here you can see the output of two merged Shapefile of Road and Rail Route of India.
1.)Road Shapefile of india
2.) Rail Route Shapefile
3.) Final Output : Merged Shapefile :
Hope this helps you to merge two or more shapefile with the help of QGIS tool. You can also find QGIS operation to convert Shapefile to GeoJSON, convert kml to Shapefile and Shapefile to KML. If you find any problem on implementing the above steps do comment below. Your suggestions are always welcome as comment.
c++ program to read shapefile header. Shapefile can be read if you know the format of Shapefile files i.e of .shp, .shx, and .dbf file. ESRI provided technical description of the Shapefile files which describes very clearly about the format of storage of data in all three formats i.e .shp, .shx and .dbf. You can also interpret and take the advantage to make simple GIS tool which can read and edit Shapefile properly. Here is the link of the Shapefile technical descrption : ESRI Technical Discription. This program reads only Shapefile .shp header, which includes file code, length, version, bounding box coordinates and the type of shape Shapefile you loaded. You can also find simple program that just reads about bounding box of that shapefile. The code is explained with the comments provided in all function that you found.
c++ program to read shapefile header:
#include<iostream>
#include<stdio.h>
#include<conio.h>
usingnamespace std;
class ByteConverter
{
public:
//Convert 32 bits which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticint32_t bigEndianIntRead(char *fileBuf, int startIndex)
{
return (((fileBuf[startIndex + 0] & 0xff) << 24) | ((fileBuf[ + 1] & 0xff) << 16)| ((fileBuf[startIndex + 2] & 0xff) << 8) | ((fileBuf[startIndex + 3] & 0xff)));
}
//Convert 32 bits which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticint32_t littleEndianIntRead(char *fileBuf, int startIndex)
{
return (((fileBuf[startIndex + 3] & 0xff) << 24) | ((fileBuf[startIndex + 2] & 0xff) << 16) | ((fileBuf[startIndex + 1] & 0xff) << 8) | ((fileBuf[startIndex + 0] & 0xff)));
}
//Convert 64 bits or 8 Byte which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticdouble littleEndianDoubleRead(char *fileBuf,int startIndex)
{
double convert;
char *add;
int j;
add = newchar();
j=-1;
for(int i=startIndex; i<startIndex+8; i++)
{
j++;
add[j] = fileBuf[i];
}
convert = *reinterpret_cast<double * const>(add);
return convert;
}
};
//Class HeaderShapefile have all funtion implemented to desribe every field of shapefile header file.
class HeaderShapefile
{
public:
//filecode describes the code of .shp file. As described in Shapefile Technical description of ESRI, filecode
//value is always constant and should have 9994 value.
staticint32_t fileCode(char*fileBuf, int startIndex)
{
return ByteConverter::bigEndianIntRead(fileBuf,startIndex);
}
//File length contains Length of the file field
staticint32_t fileLength(char*fileBuf, int startIndex)
{
return ByteConverter::bigEndianIntRead(fileBuf,startIndex);
}
//Version function
staticint32_t version(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianIntRead(fileBuf,startIndex);
}
//Function shapeType describes the type of the shape. It returns an 32 bit integer
//value. This integer is then matched with the cooresponding shape as described in ESRI shapefile pdf
staticint32_t shapeType(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianIntRead(fileBuf,startIndex);
}
//This remaning funtion of the class will calculate the bounding box coordinates of the shapefile.
//Following values i.e x and y minimum and maximum values also with z and m minimum
// and maximum values are obtained.
staticdouble dimensionXMin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf,startIndex);
}
staticdouble dimensionYmin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionXmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionYmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionZmin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionZmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionMmin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionMmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
};
class SizeOfFile
{
public:
//This function finds the size of file in Byte
staticlong sizeOfFiles(FILE *file)
{
long l, e;
l = ftell(file);
fseek(file, 0, 2);
e = ftell(file);
fseek(file, l, 0);
return e;
}
};
int main()
{
int32_t filecodes, fileLengths, shapeTypes, versions;
double xmin, ymin, xmax, ymax, mmin, mmax, zmin, zmax;
string shape;
char *filePath = "map.shp";
char*fileBuf; // Pointer to our buffered data
FILE *file = NULL; // File pointer
// Open the file in binary mode using the "rb" format string
// This also checks if the file exists and/or can be opened for reading correctly
if ((file = fopen(filePath, "rb")) == NULL)
cout << "Could not open specified file" << endl;
else
cout << "File opened successfully" << endl;
// Get the size of the file in bytes
long fileSize = SizeOfFile::sizeOfFiles(file);
// Allocate space in the buffer for the whole file
fileBuf = newchar[fileSize];
// Read the file in to the buffer
fread(fileBuf, fileSize, 1, file);
// Now that we have the entire file buffered, we can take a look at some binary infomation
cout<<"File size = " <<fileSize;
cout<<"File size get = "<<fileBuf;
filecodes = HeaderShapefile::fileCode(fileBuf,0);
fileLengths = HeaderShapefile::fileLength(fileBuf,24);
versions = HeaderShapefile::version(fileBuf,28);
shapeTypes = HeaderShapefile::shapeType(fileBuf,32);
xmin = HeaderShapefile::dimensionXMin(fileBuf,36);
ymin = HeaderShapefile::dimensionYmin(fileBuf,44);
xmax = HeaderShapefile::dimensionXmax(fileBuf,52);
ymax = HeaderShapefile::dimensionYmax(fileBuf,60);
zmin = HeaderShapefile::dimensionZmin(fileBuf,68);
zmax = HeaderShapefile::dimensionZmax(fileBuf,76);
mmin = HeaderShapefile::dimensionMmin(fileBuf,84);
mmax = HeaderShapefile::dimensionMmax(fileBuf,92);
/*****************HEADER SHAPEFILE DETAIL*********************/
cout<<endl<<"/*****************HEADER SHAPEFILE DETAIL*********************/";
cout<<endl<<"File code = "<<filecodes<<endl;
cout<<"File Length = "<<fileLengths<<endl;
cout<<"Version = "<<versions<<endl;
//This shapefile shapetypes can be found in the technical discription.
switch(shapeTypes)
{
case0:
shape = "Null Shape";
break;
case1:
shape = "Point";
break;
case3:
shape = "Poly Line";
break;
case5:
shape = "Polygon";
break;
case8:
shape = "MultiPoint";
break;
case11:
shape = "PointZ";
break;
case13:
shape = "PolyLineZ";
break;
case15:
shape = "PolygonZ";
break;
case18:
shape = "MultiPointZ";
break;
case21:
shape = "PointM";
break;
case23:
shape = "PolyLineM";
break;
case25:
shape = "PolygonM";
break;
case28:
shape = "MultiPointM";
break;
case31:
shape = "MultiPatch";
break;
default:
shape = "Wrong match found";
break;
}
cout<<"Shape Type = "<<shape<<endl;
cout<<endl<<"************* Bounding Box **************"<<endl;
cout<<"X minimum = "<<xmin<<endl;
cout<<"Y minimum = "<<ymin<<endl;
cout<<"X maximum = "<<xmax<<endl;
cout<<"Y maximum = "<<ymax<<endl;
cout<<"Z minimum = "<<zmin<<endl;
cout<<"Z maximum = "<<zmax<<endl;
cout<<"M minimum = "<<mmin<<endl;
cout<<"M maximum = "<<mmax<<endl;
cin.get();
delete[]fileBuf;
fclose(file); // Almost forgot this
return0;
}
Get minimum Bounding box of shapefile c++ program. This program not only reads shapefile, but makes you aware to read other files, i.e files which have .dbf, .exe, .png etc format without having software. Before that you should know about bits, byte order, and conversion of bytes to other data types and most importantly the format of the files that you want to read.
A shapefile stores non topological geometry. ESRI shapfile mainly contains 3 files, i.e .shp, .shx and .dbf files. To get bounding box details of shapefile, we need to concern about .shp file format. You can find all the technical discription of shapefile, provided by esri in their website.
Shapefile .shp has 3 parts, i.e file header, record header and record contents. File header stores the bounding box of shapefile. You can also see c++ program to read shapefile header. Here is the program:
Get minimum Bounding box of shapefile c++ program
#include<iostream>
#include<stdio.h>
#include<conio.h>
usingnamespace std;
class ByteConverter
{
public:
//Convert 32 bits which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticint32_t bigEndianIntRead(char *fileBuf, int startIndex)
{
return (((fileBuf[startIndex + 0] & 0xff) << 24) | ((fileBuf[ + 1] & 0xff) << 16)| ((fileBuf[startIndex + 2] & 0xff) << 8) | ((fileBuf[startIndex + 3] & 0xff)));
}
//Convert 32 bits which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticint32_t littleEndianIntRead(char *fileBuf, int startIndex)
{
return (((fileBuf[startIndex + 3] & 0xff) << 24) | ((fileBuf[startIndex + 2] & 0xff) << 16) | ((fileBuf[startIndex + 1] & 0xff) << 8) | ((fileBuf[startIndex + 0] & 0xff)));
}
//Convert 64 bits or 8 Byte which is stored in BigEndian format to integer. This is performed with the help of bit operation i.e left shifting and operating or.
staticdouble littleEndianDoubleRead(char *fileBuf,int startIndex)
{
double convert;
char *add;
int j;
add = newchar();
j=-1;
for(int i=startIndex; i<startIndex+8; i++)
{
j++;
add[j] = fileBuf[i];
}
convert = *reinterpret_cast<double * const>(add);
return convert;
}
};
//Class HeaderShapefile have all funtion implemented to desribe every field of shapefile header file.
class HeaderShapefile
{
public:
//This funtion of the class will calculate the bounding box coordinates of the shapefile.
//Following values i.e x and y minimum and maximum values also with z and m minimum
// and maximum values are obtained.
staticdouble dimensionXMin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf,startIndex);
}
staticdouble dimensionYmin(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionXmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
staticdouble dimensionYmax(char*fileBuf, int startIndex)
{
return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
}
};
class SizeOfFile
{
public:
//This function finds the size of file in Byte
staticlong sizeOfFiles(FILE *file)
{
long l, e;
l = ftell(file);
fseek(file, 0, 2);
e = ftell(file);
fseek(file, l, 0);
return e;
}
};
int main()
{
int32_t filecodes, fileLengths, shapeTypes, versions;
double xmin, ymin, xmax, ymax, mmin, mmax, zmin, zmax;
string shape;
char *filePath = "map.shp";
char*fileBuf; // Pointer to our buffered data
FILE *file = NULL; // File pointer
// Open the file in binary mode using the "rb" format string
// This also checks if the file exists and/or can be opened for reading correctly
if ((file = fopen(filePath, "rb")) == NULL)
cout << "Could not open specified file" << endl;
else
cout << "File opened successfully" << endl;
// Get the size of the file in bytes
long fileSize = SizeOfFile::sizeOfFiles(file);
// Allocate space in the buffer for the whole file
fileBuf = newchar[fileSize];
// Read the file in to the buffer
fread(fileBuf, fileSize, 1, file);
// Now that we have the entire file buffered, we can take a look at some binary infomation
cout<<"File size = " <<fileSize;
xmin = HeaderShapefile::dimensionXMin(fileBuf,36);
ymin = HeaderShapefile::dimensionYmin(fileBuf,44);
xmax = HeaderShapefile::dimensionXmax(fileBuf,52);
ymax = HeaderShapefile::dimensionYmax(fileBuf,60);
/*****************MINIMUM BOUNDING BOX OF SHAPEFILE DETAIL*********************/
cout<<endl<<"\n/*****************MINIMUM BOUNDING BOX SHAPEFILE DETAIL*********************/\n\n";
cout<<"X minimum = "<<xmin<<endl;
cout<<"Y minimum = "<<ymin<<endl;
cout<<"X maximum = "<<xmax<<endl;
cout<<"Y maximum = "<<ymax<<endl;
cin.get();
delete[]fileBuf;
fclose(file); // Almost forgot this
return0;
}
Output of Get minimum Bounding box of shapefile c++ program
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok