Get minimum Bounding box of shapefile c++ program

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>

using namespace 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.
    static int32_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.
    static int32_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.
    static double littleEndianDoubleRead(char *fileBuf,int startIndex)
    {
        double convert;
        char *add;
        int j;
        add = new char();
        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.


    static double dimensionXMin(char*fileBuf, int startIndex)
    {
        return ByteConverter::littleEndianDoubleRead(fileBuf,startIndex);
    }

    static double dimensionYmin(char*fileBuf, int startIndex)
    {
        return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
    }

    static double dimensionXmax(char*fileBuf, int startIndex)
    {
        return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
    }

    static double dimensionYmax(char*fileBuf, int startIndex)
    {
        return ByteConverter::littleEndianDoubleRead(fileBuf, startIndex);
    }
};

class SizeOfFile
{
    public:
        //This function finds the size of file in Byte
    static long 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 = new char[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
	return 0;
}

Output of Get minimum Bounding box of shapefile c++ program

Get minimum Bounding box of shapefile c++ program
Get minimum Bounding box of shapefile c++ program

What is GIS | Geographic Information System

What is GIS | Geographic Information System? GIS is an Abbreviation of Geographical Information System or Geo-spatial Information Systems or Geologic Information Systems (also Geo-spatial database management). As the name suggest ‘GIS’ is related to geographical information. But before explaining what is GIS?, we should know about what Geographic information means? Geographic information is knowledge of where something is. It defines the spatial reference of the element or matter present on earth surface.

As it is said “a picture is worth a thousand words”, Maps plays a lead role in defining geographical information. We control map to guide someone to know the actual  route, exact destination, its distance, its elevation, its surroundings etc. Geographical information is needed to Government, Industries, Business markets, Tourism and services sector,  Education, Agriculture, Health, Disaster Management,  Transportation and also to a common people. It makes us easy to locate an area of industries, day care center, restaurant, mall, head quarter or any other specific place.  It solves everyday issues. What is the shortest route to get to work? How can i get to hotel? Where is the location of my new office? So, Geographical information is part of our lives. But how can we get geographic information and What is GIS?

Geographic Information System
Geographic Information System

What is GIS | Geographic Information System?

GIS is a tool to acquire geographic data and produce Geographic information. Generally Information about those features are stored in tabular formGIS 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”.
GIS enables us to connect dynamic relations between spatial data (geo referenced) & relational data (attribute of features in tabular forms). It enable us to make models of reality, of the ground and to answer question such as what is where? Main component of Geographic information system are: Hardware, Software, Data and People.

You may also be interested in knowing GIS Uses and Application in different industries.

Geographic Information System
Geographic Information System
Some organizations defined GIS – Geographical Information System as :

A geographic information system (GIS) is a computer-based tool for mapping and analyzing things that exist and events that happen on earth. GIS technology integrates common database operations such as query and statistical analysis with the unique visualization and geographic analysis benefits offered by maps.” ~ ESRI

“GIS is an integrated system of computer hardware, software, and trained personnel linking topographic, demographic, utility, facility, image and other resource data that is geographically referenced.” ~ NASA 

Google Map can be considered as an Example of GIS tool. You can have a look over 35+ Google Tips and tricks.

The demand for GIS tools is continuously growing. One of such tool is QGIS(Quantum GIS), and is a cross platform, free open source desktop application. From a citizen to an organization, every body needs Geographic information. You can find here advantage of GIS and Why GIS. I hope that reading this post will make you to learn more about Geographic information System. I would appreciate your comment, if you like to share more ideas related to the same topic.

Convert Shapefile to kml by QGIS

Convert Shapefile to kml by QGIS. Shapefile is ESRI based Vector file, While KML or Keyhole Markup Language is file which models and stores geographic features for display in Google Earth or Google maps. Many times you may require to convert the required shapefile to KML file, so that you can deploy the same over Google Earth for some reason. QGIS (Quantum GIS) is an open source GIS (Geographic Information System) tool, which let you upload the Shapefile and convert the same to KML file. Similarly ogr2ogr is also an open source tool, which let you to convert between GIS data formats. Look over shp to kml convert using ogr2ogr tool which can be marked as an alternative to this article. You can also see to convert Kml to Shapefile and also Shapefile to GeoJSON in QGIS.

You can also try Online conversion instead of QGIS MAPOG Online converter tool

You can also go through other tutorial in QGIS.

Free SHP to KML conversion using GIS Converter Tool in one click.

Here are the steps to convert shapefile to kml:

Convert Shapefile to kml in QGIS

Convert Shapefile to kml by QGIS
Convert Shapefile to kml by QGIS

Convert Shapefile to kml in QGIS:

1.) Open up the installed QGIS.
2.) In the menu bar, select Layer and press Add Vector Layer. Now select the source type in the pop up window opened as File and browse the source Shapefile(Note:Your shapefile .shp, .dbf and .shx file should be in same folder). Press open.

Convert Shapefile to kml by QGIS
Convert Shapefile to kml by QGIS

Select the coordinate reference type system if coordinate reference window appears and press ok.

3.) Right click on the name of Shapefile layer Appearing in the Left upper side and select save as:
or
You can select layer from the menu, and click on save as. (Image below provide demonstrate both the options with the indicator displayed)

Convert Shapefile to kml by QGIS
Convert Shapefile to kml by QGIS

4.) In the Save vector layer as window, Select the format from the drop down box as Keyhole Markup Language(KML) and browse the folder in which you want to save the KML file . Name it and Press OK.

Convert Shapefile to kml by QGIS
Convert Shapefile to kml by QGIS

Your KML file will be Exported in the directed folder. You can Verify the generated KML file for its validity by uploading the generated KML file to QGIS or uploading KML file in Google map. You may soon find the tool and the coding for the same on this site.

You may check in converting Geojson to Topojson and Shapefile to TopoJSON file. Hope this may help you in converting Shapefile to KML file easily. If you still find problem in converting the file, do let me know by commenting below.

Introduction of QGIS 3.2.1

Convert KML to Shapefile in QGIS

Convert KML to Shapefile in QGIS. KML or Keyhole markup language file which store and models geographic features for displaying in Google earth or Google maps, which is not common in use for Map makers and GIS programmers. ESRI made a standard Vector file i.e Shapefile which is most commonly adopted in the field of GIS (Geographic Information System), and this made me to convert KML to shapefile, created for learning purpose from Google map. QGIS (Qunatum GIS) is an open source GIS tool, which provide you a function to convert the KML file to Shapefile, and Vice versa i.e Shapefile to kml file.

You might be looking for converting KML to shapefile when you get the data from third party or from website which download free GIS map data but in different formats which can also be converted to.

If you still don’t prefer using QGIS or GDAL utility to convert the file from kml to shp, then you should also look for KML to Shapefile Conversion using Online Tool.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

You might be interested in seeing QGIS tutorials.

Instead of QGIS you can also try Online Converter tool here MAPOG Online converter tool

Convert KML to Shapefile in QGIS:

1.) Open up the installed QGIS.
2.) Select Layer in the menu bar and press Add Vector Layer or select the shortcut icon. Browse the KML file, in the Source DataSet type, in the pop up window opened.(While browsing, select either all files or Key Hole Markup language in file type). And press Open.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

Select the coordinate reference type system if coordinate reference window pop ups and press ok.

3.) In the Layer file appearing in the left upper side, right click on it, and select save as:

or

you can either select layer from the menu and click on save as.

Convert KML to Shapefile in QGIS`
Convert KML to Shapefile in QGIS

4.) In the Save vector layer as window, Select the format from the drop down box as ESRI Shapefile(OGR) and browse the folder in which you want to save the shapefile. Name it and Press OK.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

Try our tool for conversion: MAPOG Online converter tool

Your Shapefile i.e .shp, .shx and .dbf file will be created in the directed folder. Upload your KML file in your map browser tool, or try our tool, to verify whether the tool preserves all attributes and elements or not. You may also see how to merge two shapefile in QGIS. Or if you are looking to convert that shapefile to kml to Topojson then you can look for GeoJSON to Topojson and Shapefile to GeoJSON and shapefile to TopoJSON. Do comment below if you are getting any problem in converting the file.

QGIS 3.2.1 for Beginners 

How to remove gaps from a polygon layer file in ArcGIS?

How to remove gaps from a polygon layer file in ArcGIS? While preparing polygon layer file following various stages of layer preparation like

This is what making it suitable to be used by a web application as an input. While layer file should be accurate with minimum error before deploying or issuing the same to the client, which requires lots of time to make.  Obviously no client for project submission will tolerate the delay and so this task time is to be reduced. So this blog post will deal with, how reduce time duration of preparing a layer file?

If you are new to ArcGIS, I recommend you to Look for  FAQ  and Interview of ArcGIS Tutorial.

Remove gaps from a polygon layer file in ArcGIS:

This is quite possible by using ArcGIS inbuilt tools and this can be accomplished by understanding the need and using appropriate tool. A number of errors are generated at various stages of layer preparation like gap in a feature, overlap with other feature, self over and many more. These errors can be detected by selection tool or by running topology rules for every layer. But the question is removal of these errors. If there are just a hundred of polygons in layer one can remove it manually zooming on to every gap and overlap. But if there are a million or more number of polygons in a layer for example if a village has 1000 parcel on an average in a district of 600 villages, the total number of parcel in a district comes out to be 6 lakh. It takes almost equal time in preparing an error free file as much time used for preparing the layer file.

For this one task i.e. removal of gaps in a feature can be made easy and can be performed in less time by the use of a tool named Eliminate present in the Data Management toolset of ArcGIS.

Steps to remove gaps from a polygon layer file in ArcGIS

remove gaps from a polygon layer file in ArcGIS
Detection of Gaps in the polygon File

 

1.) First all gaps in a layer file should be converted to polygons using auto complete polygon tool from the editor bar.

remove gaps from a polygon layer file in ArcGIS: Forming polygons from gaps using Auto-complete tool
Forming polygons from gaps using Auto-complete tool

2.) Then these polygons should be copied to layer file.

remove gaps from a polygon layer file in ArcGIS: Copying gap polygon to main file.
Copying gap polygon to main file.

3.) Selecting all gaps in the form of polygon apply eliminate tool from data management toolset on the layer file.

remove gaps from a polygon layer file in ArcGIS: Applying Eliminate tool on the polygon file to remove gaps.
Applying Eliminate tool on the polygon file to remove gaps.

4.) The gaps are merged with the neighboring polygon having larger area or longer shared border.

In this way the work of one month can be reduced to one week or lesser. This tool should be used for removal of small gaps only. As for larger gaps the decision of merging the gap polygon in which neighboring polygon should be done by visual interpretation and analytically perspective.

If you are dealing with Raster files with ArcGIS and with multispectral band files, you may look for Pan-sharpening image using ArcGIS.

Hope this tutorial would help you to remove gaps from a layer file in ArcGIS.  While I always works in Open Source tool in GIS, I would like to make you check the QGIS and QGIS tutorials.

If you are facing any problem related to the above, then do comment below, we would look for the problem and suggestion for the same.