Geographic Information System is a system intends to capture, Store, manage, manipulate, analyze the data. GIS system integrate data and convert it into maps. And as we know visuals are more powerful than words so it makes our understanding of data fast.
What is GIS ?
GIS is a tool to acquire geographic data and produce Geographic information. And generally those information are stored in tabular form. GIS can be defined as a “computer information system that can input, store, manipulate, analyze, and display geographically referenced (spatial) data to support decision making processes”.
Simply GIS visualize your data as a map. And these visualization makes the study interesting with interactive maps. It saves the time and make the system decisiveness that is main requirement.
Upload Excel latitude longitude in QGIS as Spatial Layer and save as Shapefile. When you have collection of point data i.e latitude and longitude, stored in Excel file in different column, then you can easily view and Save it as Vector layer file in QGIS(Quantum GIS). Well after uploading Excel latitude longitude values as spatial layer, you are only one step back, to convert the Excel data in Shapefile or KML or any vector file.
Before starting the exercise, you need to have a data excel file, which should be convert to Comma Delimited .csv file(Depends upon your choice, Comma delimited is common). To convert Excel file in .csv file, open the file and select save as type “CSV (Comma delimited) (*.csv)”. Name the file and save it.
Note: If you are using old version of QGIS then, do download and install plugin “Add delimited text layer”. To install a plugin,
Go to Plugins -> Manage and install Plugins, search for “Add delimited text layer“, and install it.
Steps to Upload Excel latitude longitude in QGIS as Spatial Layer
You should have an already downloaded and installed QGIS , before going through this steps.
1.) Open up QGIS and click on layer, from menu tab and select Add delimited text layer.
2.) Browse the already created .csv excel file, which has latitude and longitude coordinates.
3.) By default, if your column is named as latitude and longitude, then, X will be mapped to longitude and Y will be mapped to latitude. Select OK after mapping X and Y.
4.) Select the desired coordinate reference system and press OK, the file get uploaded as Spatial layer in QGIS.
Well if you want to convert this uploaded Spatial layer in Shapefile or KML or any other vector file, select on layer, and click on save as. In the open window, select the desired format in which you want to save the file. Done. I suppose this tutorial would be helpful to you. If you are facing any problem, then do comment below.
Create save Export and download KML file from Google map. Google map or Google Earth makes use of KML or keyhole markup language, to display features, which actually stores geographic features in Vector format. You can create KML file from Google map for mapping and displaying the same by again uploading the same KML file on Google mapor created KML file converted from Shapefile. Before creating KML file on Google map, you should have an Google account and should be logged in wtih that. So that once KML file created, you would be able to see it in My Places of Google map after wards. You can also look link for how to create KML file from Google earth.
Create save Export and download KML file from Google map
Note: Google provides Google map and this kind of service for free. But i suppose, if you are going to extract and create KML file with your program in bulk, then Google may notify you for the same.
Steps to Create save Export and download KML file from Google map
1.) Open Google map and signin with all in one Google account.
2.) Click the setting button which appears at the bottom right corner of map as shown below (New Google map), and select My places. Or if you are using old map theme, then choose my places from top right corner.
Update: If you are not getting my places option from here, then you click on left side Menu icon (three lines) and select your places and tap on Maps.
Once you clicked your places select the Maps.
3.) Now click on Create Map (in the last with blue font) which will open new link in new tab and select Create Map again.
4.) Perform and create a map, with the help of tool bars as shown below, Which has following options i.e
Undo
Redo
Select item
Add a Marker
Draw a line
Add direction
Measure distance and Area.
5.)Name your map, by clicking on untitled map. You can now Add layers too, i.e for Adding Markers, which will be treated as point feature, make different layer while for Adding direction or line you can have different layer.
Note: You cannot have more than 3 layers for the same map project for Free. You need to go for Google map pro.
Here is the look, i had created 3 layers, i.e 1st one is Point layer, 2nd is Direction layer and 3rd is Line layer, as shown above.
6.) Once you are done with creating map, click the folder icon, shown just below your title of map project and Select Export as KML. Now you can export the entire map or either of the layer by selecting the options. Done.
You can now share your map you created map and can also embed it in your website. What are your plannings with the newly created KML file? Do you want to convert kml file to shapefile. Do comment below, how you use KML file so that me as well as our visitors will come to know about new technology and uses. Your valuable suggestion and questions are always welcomed by commenting below in the section provided.
Upload or import KML file on Google map. KML or Keyhole Markup Language is file which models and stores geographic features for display in Google Earth or Google maps. You can upload and view the KML file you already created on Google Map, to verify Whether the overlapped feature of KML uploaded has proper valid geometry or not in respect to the Google Map Feature. Before uploading the KML on Google Map, you need to have a Google account. The file uploaded will be saved to “My Places“, you don’t need to upload it twice when require. If you are not having an already built KML file, you can look at How to create KML file on Google mapor How to create KML file on Google earth.
You can also tryMAPOG Map Analysis Product to upload or import the KML file Online and View on map for free. Along with that you can further process the GIS Data with different operation and finally create a brief Analysis report.
Upload or Import KML file on Google map
Here are the steps to upload or import KML file on Google map
1.) Open Google map, and sign in for all in one Google account. 2.) Move to My Places, a button on left upper side of Google map, and then select the Create with classic My Maps as shown below.
3.) Select import option, and then browse the KML file you want to upload on Google map.
4.) Done, edit the title and description. You can make the map public or private. Save the map.
Note: You can upload KML file of Maximum size of 3MB on Google Map, as per Google instructions. While i found some lose of data while uploading KML file of greater than 2MB. Alternative to Google map is Google earth for the same.
The map saved, can be again viewed and downloaded by going to “My Places -> click on the title of map -> then the KML” option appearing just below the title of your map. You can also find the tutorial to convert KML to Shapefile and Shapefile to KML, with the help of QGIS an open source GIS tool. Want to know more technically on the KML and shapefile, i will provide you soon, related tools and coding for the same with explanation.
Geo-Referencing raster image in QGIS with respect to vector file. Geo referencing is the process used to provide geographical information to the raster images like cadastral maps, physical maps and satellite imagery with the help of projection or coordinate system. As an ordinary raster image formed by the number of pixel which has no particular size, Geo-referencing gives a particular coordinate to a pixel with respect to the ground point, it represents. Thus every individual pixel get its coordinate and size.
There are many GIS tools which perform Geo-referencing like ArcGIS, PCI Geomatica, Erdas Imagine, QGIS etc. Here Geo-referencing is shown in QGIS:
Geo-Referencing raster image in QGIS with respect to vector file
In QGIS first start with loading the vector file in QGIS environment by click on add vector layer.
Once the vector layer is loaded it can be given symbology by right click on the layer in the layers window. For geo-referencing the click on the georefrencer tab present at bottom of the QGIS environment window as shown below.
This will open a new window named Georeferencer . In this window open the raster image to be referenced by click on the tab open raster layer present on the top left corner.
When raster is open in the georefrencer window to add control points according to which raster will be georefrenced click on the add point button on the tool bar. Control points are the coordinate points of the ground or the projected coordinates.
These control points may be enter manually or from the map canvas as here reference is vector layer which is present on the map canvas. click on from map canvas on Enter map coordinates window.
This will open the map canvas or main QGIS window , select the point of reference on the vector layer.
Selection of point on vector layer will fill in the XY coordinates on the Enter map coordinates window. In this way first control point is marked.
Following same procedure select more and more points with minimum residual error which is generated by the tool and shown in the GCP table window.
Residual error is the difference between the actual coordinate of the control points and the geographic coordinate created by control points. The residuals represent the level of accuracy of the georefrenced images.
These georefrenced images can be placed in the layers of any Geographic information system and can be used to locate points on the ground. Feel free to comment below, if you are having any problem to Geo-Referencing raster image or if you want to discuss for the same topic.
How to connect QGIS to oracle spatial. QGIS can now connects to oracle to view spatial data and process it, great news for an open source software believers. As a newbie to QGIS user, i can just give a way, that i connected to it.
How to connect QGIS to oracle spatial :
1.) Firstly you should have downloaded QGIS with GDAL/OGR required oracle library for bridging oracle connectivity and must have installed oracle on your machine. If oracle library is not installed, see the following post : Install QGIS for oracle connection via OSGeo4w.
2.) After starting the QGIS application, select Add Vector Layer as shown below:
3.) A new window will pop up on clicking the above, now select Database Radio button as appeared in Source. Now in Database Type drop down box, select Oracle Spatial. If you have properly installed QGIS with the requirement stated above Oracle spatial would be present.
4.) In the same Add Vector Layer window, select new button under connection label. This will open up a simple form with following Connection information to be filled, i.e, Type, Name, Host, Database, Port, Username and Password.
Here is the way to fill up the information :
a.) Type : In type select oracle spatial from drop down box.
Type : Oracle Spatial
b.) Name : Type in any desired name you wish to give to connection
c.) Host : For finding your host name, make a query in your oracle sql plus application as written below:
SELECT UTL_INADDR.GET_HOST_ADDRESS FROM DUAL;
This will return the 32 bit address as shown below in figure:
d.) Database : Write database name, i.e the name you have given while installing oracle software.
e.) Port : Enter port number i.e 1521
f.) Username : Enter username
g.) Password : Enter corresponding password of username
Now select Test Connection button. This will pop up another window which gives you an information of your successful connection, as shown below.
Note the string url connection to connect QGIS to oracle spatial and click OK.
5.) Again in the vector data layer window, select the connection that you made named as Demo and click on Open button.
6.) Now select the coordinate system for your database, which will be prompted after proceeding from step 5.
7.) If all steps are successfully done, you can see your spatial table, with your vector data spatial view as shown below:
Feel free to comment below, if you are having any problem to connect Oracle Spatial to QGIS QuantumGIS (Geographic information System) or if you want to discuss for the same topic. You can also see QGIS tutorial further. And yes, don’t forget to subscribe and follow on twitter for getting updates related to GIS post.
Download and Install quantum GIS – QGIS with oracle connectivity. Quantum GIS is an open source cross platform software for developing GIS (Geographic information System) solutions. QGIS connects to large set of spatial database and supports many data file formats, which enable us to visualize data, analyze data, edit data and generate a final map. QGIS is a best alternative to ESRI ARCGIS Desktop and is available for free under GNU General Public License. You can download and install Quantum GIS from either of the two way, i.e by standalone installer software or you can install it with OSGeo4w installer. OSGeo stands for open source geospatial foundation which include many open source projects, geospatial libraries, desktop application and web mapping.
While QGIS can be installed by one click with QGIS standalone installer, but it is better to install QGIS with OSGeo4w software as it helps you to install many libraries and packages with QGIS. As this post topic is concentrating for QGIS with oracle connectivity, so, for that reason we need to install GDAL/ORG library with QGIS , and it would be easy for you to install with support of OSGeo4w installer.
You might be interested in looking at QGIS tutorial.
If you have already done with this, you can see post to connect qgis with oracle : QGIS connect with oracle.
Download and Install quantum GIS – QGIS with oracle connectivity:
a.)Run the installer and select “advanced Install” and select “Next”.
b.)Choose Install from internet when choose download source appears, as shown below.
c.)Select root directory, then next and select package directory and select next. Choose your internet connection. Default installation directory will be, C:\OSGeo4W. If you want to change the directory you can choose and select the same, as shown below.
2.)Select package to install will be prompted on the same window, Wait, don’t select next. You need to select the desire packages from the following open source utilities, enlisted below:
a.) Command line utilities: Dont need to select any utitlies from this for our above aim.
b.) Desktop utilities: Here, select the following libraries:
i.) grass : Geographic Resource Analysis Support System
ii.) qgis : Quantum GIS (desktop)
iii.) qgis-dev : Quantum GIS rightly build of the master
c.)Libs (Library): By selecting above desktop utitlies, some libraries which are connected with above desktop utilites will get selected by default. Let it be selected and dont make any change in that. For getting oracle connection, some of libraries you need to select by clicking and making cross across that. This libraries are as follows:
i.) gdal : The GDAL/OGR library and command line tools
ii.) gdal-ffegdb : OGR fileGDB driver
iii.) gdal-oracle : OGR OCI and GDAL GeoRaster Plugins for Oracle
iv.) gdal-oracle 10g: Oracle 10g Plugin for GDAL(OGR)
v.) gdal 16-oracle : OGR OCI and GDAL Geo Raster Plugins for Oracle
d.)Web and Web application.
2.) After selecting all libraries, click on next. This step will connect you to internet and install all the library and utilities that you have selected. This may take some time to download and install.
3.) All done. Now Open Quantum GIS and select add vector layer. Now select database in vector layer window and see if oracle spatial is on the list or not.
If you are having any problem to Download and Install quantum GIS – QGIS with oracle connectivity, comment on below. As i am also a newbie to QGIS, we will discuss and make out some proper solution.
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.
Pan-sharpening images using ArcGIS. Pansharpening is the process which makes use of panchromatic image and some bands of multispectral image to result in a high resolution image. Pansharpening stands for panchromatic sharpening. Here sharpening basic refers to increase the spatial resolution of multisprectral image or color image. This technique of increasing resolution has been used by various organisations that provide satellite imagery of high resolution like Google images etc.
A multi spectral image is captured with high spectral resolution as it is displayed in different colors while the panachromatic image is captured with high spatial resolution and provides a cluster of red, green and blue pixels resulting in a gray scale image.Now the question is how we get a pan-sharpened image? The possible answer by an ArcGIS user can be it’s just a tool to run. But is it that easy? No. Both the images has to be pre-processed before applying this tool which comes out to be a tough process if you have a number of tiles for your area of interest.
Pan-sharpening images using ArcGIS
First of all start with the multispectral image let’s say a cartosat image with a resolution of 1 arc sec. Start with stacking of different bands(either all even or all odd number) in Erdas imagine. Perform Geo-referencing of each tile making one tile static.
Then mosaicing of tiles in ArcGIS using Mosaic tool of data management tool set. The panchromatic image will be a single band image say panchromatic band of lansat image with 15 m resolution so it does not need to be stacked. So just mosaic all the tiles of panchromatic image. Geo-reference both multispectral and panchromatic image accordingly so that they both are same spatial location without any spatial difference.
Now apply tool create pan-sharpened raster data-set of data management tool-set.
Thus a multispectral image with high spatial resolution is obtained. As panchromatic image possess high spatial information and multispectral image possess good color information. Fusion of the two brings out a colored image of high resolution which can be used as a product for any client who want an detailed view of his area of interest.
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