Shp to SQL – Convert Shapefile to postgersql file

Shapefile shp can be converted in pgsql file using shp2pgsql command line tool. Sometimes you have a shapefile and want to upload the same in postgresql database, you can directly use shp2pgsql command and upload shapefile to postgis. While there might be possibilites that you would not be having access to your production server to install shp2pgsql command line tool, so you can convert shapefile into a sql file and then upload the same to Database. Lets check how to convert shp to sql file.

Conversion using shp2pgsql – shp to sql

Try Our Tool MapOG Converter for SHP to SQL conversion

For executing this command you need to have shp2pgsql utility in your system. You can check the presence of utility by typing shp2pgsql in terminal. If this utility is present then you will see the following result.

Convert- Shapefile Shp to SQL postgersql file
Convert- Shapefile Shp to SQL postgersql file

Shp2pgsql has some option, some for them are used here. Option -I creates a spatial index on the geocolumn. Option -s specifies the SRS (Spatial Reference system) in EPSG (European Petroleum Survey Group) number.

–> shp2pgsql -I -s 4326 shpfilepath.shp output_table_name > output.sql

After executing this command, you can run the sql file in postgres to insert data in table.

Code – Convert- Shapefile Shp to SQL postgersql filE

If you know to read binary files by knowing its format, you can easily read and convert shapefile to sql, as shapefile has an open standard file format. For instance, you can look over tutorial I had created in how to read shapefile bounding box using c++ or reading shapefile header.

In codeigniter framework of php, we can use shell_exec() by providing command as input. In command we need to export PGPASSWORD, where password is supplied.
While writing command in PHP, we need to take SRS (Spatial Reference system) of shape file, input shapefile and output file name from the user. The passwrThen query is written in string format and supplied to shell_exec() method. This method returns output as string and generates file in folder.

PGPASSWORD behaves the same as the password connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using the ~/.pgpass file. For more detail you can visit https://www.postgresql.org/docs/9.1/static/libpq-envars.html link.

public function shptosql($shpfilepath,$output){
$srs=4326;
$query=”export PGPASSWORD=’download1′; shp2pgsql -I -s $srs $shpfilepath $output > $output.sql”;
shell_exec($query);
}

You can check for SQL file in the same folder where shapefile is kept. The generated result will look like-

Convert- Shapefile Shp to SQL postgersql file
Convert- Shapefile Shp to SQL postgersql file

Change coordinate system – shp to sql

While converting shapefile into sql, you can not change the coordinate system. But you can execute this process in two steps as-

  1. Change the coordinate system of shapefile

–> ogr2ogr -f ‘ESRI shapefile’ -s_srs EPSG:4326 (old EPSG number) -t_srs EPSG:32643 (new EPSG number) new_shapefile.shp India_raods.shp

2. Then convert shapefile into sql

–> shp2pgsql -I -s 32643 new_shapefile.shp output_table_name > output.sql

ogr2ogr utility has some options as-

-a_srs srs_def:

Assign an output SRS

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

You may also Check Shapefile to GeoJSON, Shapefile to KML, Shapefile toconverting TopoJSON, etc conversion.

Hope you enjoyed the conversion and executed it successfully. Let us know you face any difficulty during execution of this tutorial via commenting in provided box.

Shp to MIF – Convert Shapefile to MapInfo Interchange Format

Many people uses MapServer as their Server, so they might need to store shape file as MIF (Map Info File) file on server. The OGR Simple Features Library allows MapServer users to display several types of vector data files in their native formats. For example, MapInfo Mid/Mif and TAB data can be seen on MapServer when using OGR support with it. So lets see how to convert Shapefile shp to mif or map info file.

Here is the online tool To convert Shapefile to MapInfo MIF 

Lets look what is MIF ( MapInfo Interchange Format) Before converting shp to Mif

MIF (Map Info File) File format used by MapInfo mapping and geographic analysis software. This stores a map visualization in a format that can be recognized by third party applications. Also used as an exchange format between GIS applications. This sometimes accompanied by a .MID (MapInfo data) file that contains additional spatial data. MID files are GIS data files created by MapInfo, a spatial data analysis and visualization software, saves spatial data for a corresponding .MIF (MapInfo Interchange Format) file. Also used for storing larger data sets not expressed in the MIF file.  What is Shapefile – collection of geometry with other supported files as shx, dbf, prj ect.

MIF file show some general property when open with QGIS software, as like :

i.) Storage type of this layer
–> MapInfo File
ii.) Description of this provider
–> OGR data provider (compiled against GDAL/OGR library version 2.2.2, running against GDAL/OGR library version 2.2.2)
iii.) Source for this layer
–> /var/www/html/PHP_pgrouting/public/shpfolder222/newwhello.mif
iv.) Geometry type of the features in this layer
–> Line
v.) The number of features in this layer
–> 19148
vi.) Capabilities of this layer
–> Fast Access to Features at ID, Presimplify Geometries, Presimplify Geometries with Validity Check
vii.) In layer spatial reference system units
–> xMin,yMin 68.4982,7.92528 : xMax,yMax 97.3348,35.5013
viii.) Layer Spatial Reference System
–> +proj=longlat +datum=WGS84 +no_defs

Convert Shapefile SHP TO MIF format using ogr2ogr utility-

ogr2ogr supports some file format MIF is one of them. You can write a simple command to convert your shapefile. Before this you must check for ogr2ogr utility. That can be done by typing ogr2pgr in your terminal.

Convert- Shapefile Shp to MIF - Map Info File
Convert- Shapefile Shp to MIF – Map Info File

If this utitlity is not available, you should run the given commands-

Convert- Shapefile Shp to MIF - Map Info File

After this please check ogr2ogr utility http://www.gdal.org/ogr2ogr.html is available now.

Now you can move for conversion. For conversion please use the given command

–> ogr2ogr -f “MapInfo File” output_file.mif Input_SHP.shp

Note-

You must have shp along with .shx and .dbf file for proper conversion.
If you only have shp file you will got an error something like.

Convert- Shapefile Shp to MIF - Map Info File
Convert- Shapefile Shp to MIF – Map Info File

And if you don’t have .dbf file, then you following error will appear

Convert- Shapefile Shp to MIF - Map Info File

Code to Convert Shp to MIF

If you are aware of reading binary file by knowing the file format, you can easily read and parse shapefile and convert the same to any other format. You can check out one such tutorial which I had created to read header of shapefile using c++ or bounding box of shapefile using c++.

This same work can be done in PHP using shelll_exec() method, which execute the command and gives output as string. The following function describes the $query variable, which takes command as string and given to shell_exec(0 method.

public function shptomif($shpfilepath,$output) {
$query=”ogr2ogr -f ‘MapInfo file’ $output.mif $shpfilepath.shp”;
shell_exec($query);
}

Output of mif file from shp file-

The Output will contain .mif and .mid files. You can open MIF file in QGIS software.

If you are using Windows operating software than you can open .mif (Map Info File) with microsoft word. The following result will appear

Version 300
Charset “Neutral”
Delimiter “,”
CoordSys Earth Projection 1, 104
Columns 7
MED_DESCRI Char(254)
RTT_DESCRI Char(254)
F_CODE_DES Char(10)
ISO Char(7)
ISOCOUNTRY Char(54)
Length_km Decimal(11,2)
Latitude Decimal(11,2)
Data line(…..)

Change Coordinate System of MIF from shp format

Converting shapefile into MapInfo File with desired coordinate system can be done by using ogr2ogr utility. It has some options related to SRS (Spatial reference system), given below-

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

So the query can be written as-

–> ogr2ogr -f ‘MapInfo File’ -s_srs EPSG:4326 (old EPSG) -t_srs EPSG:32643 (new EPSG) output_mifFile.mif Input_file.shp 

Hope this tutorial helped you to convert your shapefile to MIF (Map Info File) format. There are also various conversion available as- Shape to CSV, GeoJson to topoJson, Shapefile to Topojson , shapefile to kml, shp to ods, shp to gml etc.

Please let us know, if any find any difficulty in performing this conversion via commenting in provided box.

SHP to XLSX – Convert Shapefile to Excel

Sometimes we just need to see attribute details not geometry. So instead of carrying heavy shape files we can convert them in XLSX format. XLSX file contains on attribute detail or can say non spatial data. XLSX files are light in weight. Along with shp to xlsx convert you may also check for shp to ods, shp to csv conversion which also more focuses on attribute details.

Convert SHP to XLSX using MapOG Tool for hassle free conversion

KNOW ABOUT XLSX AND SHAPEFILE format -BEFORE CONVERTING SHP TO XLSX

An XLSX file is an Excel spreadsheet created by Microsoft Excel or another spreadsheet program. It stores data in worksheets, which contain cells arranged in a grid of rows and columns, and may also contain charts, mathematical functions, styles, and formatting. XLSX files create simple or complex mathematical models.

What is shapefile? Each object within a SHP file contains an individual geographic feature and its attributes.

Conversion from shapefile to XLSX using ogr2ogr utility-

–> ogr2ogr -f XLSX output_xlsx_filename.xlsx India_raods.shp

In command -f is file format and XLSX is driver. Then you can specify the output file name with .xlsx extension and input shapefile. This command can be executed on terminal or using PHP code. Here you must look for the ogr2ogr utility in your system. If not available you can install it using given line of code.

SHP to XLSX

Then you can check the presence of utility by typing ogr2ogr in terminal. The output will look like-

SHP to XLSX
SHP to XLSX

Note that while converting shapefile to XLSX, you must check for .shx file. If shx file is not available you may get following error.

SHP to XLSX

Shapefile has some supporting file like .dbf, .shx, .prj etc. So you must take all file whenever you perform conversion.

PHP CODE – SHP to XLSX

If you are aware of Shapefile Open description format, then you can easily access shapefile data through coding. As i have created one such shapefile minimum bounding box parser in c++. Below in the php code to execute ogr2ogr tool.

Here you can perform the conversion using shell_exec() method, which actually executes the command and returns string as output.

public function shptoxlsx($shpfilepath,$output) {
$query=”ogr2ogr -f XLSX $output.xlsx $shpfilepath.shp”;
shell_exec($query);
}

After execution of command you can check the specified folder to see generated xlsx file. The content of file will be something like-

SHP to XLSX

You may also look forward to convert shapefile to GeoJSON, Shapefile to Kml, Shapefile to topojson and etc conversions.

Hope you enjoyed the article and successfully converted shape file into XLSX file. If you have any problem regarding conversion please feel free to write in comment box.

Shp to ODS – Convert Shapefile to Open Document Spreadsheet

Shape file to ODS (Open Document Spreadsheet) conversion is required when you only need non spatial information i.e. only attribute information. This conversion need ogr2ogr utility. Or you may also you any other tool to convert shp to ods easily.

Convert SHP to ODS using MapOG Tool for hassle free conversion

What is ODS (Open Document Spreadsheet) and Shapefile-Shp to ODS CONVERT

An ODS (Open Document Spreadsheet) file is a spreadsheet. It stores data in cells that are organized into rows and columns. ODS files are formatted using the OASIS Open Document XML-based standard. Whereas shape files are collection of geometry with non spatial data as attributes.

To install ogr2ogr utility you can follow the given lines of command.

Convert Shapefile Shp to ODS- OpenDocument Spreadsheet

Then check for presence of ogr2ogr utility by typing ogr2ogr in your terminal.

Conert - SHP To ODS

Conversion- Shapefile shp to ODS (Open Document  Spreadsheet) using Ogr2ogr utility-

Now the conversion command can be executed,

–> ogr2ogr -f ODS output_ods_fileName.ods  India_raods.shp

Here, in the command -f is the option for file format.

Note that while converting shx file should be present in same folder where shp file is kept. Otherwise you will get error like-

Shapefile Shp to ODS- OpenDocument Spreadsheet

All the supported file (.shx, .prj and .dbf etc.) should be kept together while performing any conversion.

Code- Convert shp to ods

You can use any programming language to convert between shapefile to ods, if you know the format of shapefile and ods file. I have created one such minimum example of code to find the minimum bounding box of shapefile with c++ programming lanauage. Below is the php code.

public function shptoods($shpfilepath,$output) {
$query=”ogr2ogr -f ODS $output.ods $shpfilepath.shp”;
shell_exec($query);
}

The generated output can be seen in the same folder, where shapefile is kept. Output will look as-

Shapefile Shp to ODS- OpenDocument Spreadsheet
Shapefile Shp to ODS- OpenDocument Spreadsheet

You may also be interested in knowing how to convert shapefile to kml, shp to gml, shapefile to csv, shapefile to geojson, shapefile to topojson etc.

Hope this post is helpful for you to convert shp to ods document. If you find any difficulty in implementing the conversion of shapefile to open document spreadsheet, let you know by commenting below in the box provided.

SHP to GML – Convert Shapefile to Geography Markup Language

Shape file shp to GML conversion is required when you carry data from one place to another. Shapefiles are heavy as compared with GML (Geography Markup Language). On the other hand we can say that GML makes you able to do many of the same things that you would do with heavyweight desktop GIS files.

For a more in-depth guide on this topic, be sure to check out our accompanying video tutorial, where we walk you through each step visually and provide practical demonstrations.

Convert SHP to KML using MapOG Tool Converter

GML (Geography Markup Language) is about describing kinds of geographic objects. This is XML based language that contain two part- the schema that describes the document and the instance document that contains the actual data. GML schema allows users and developers to describe generic geographic data sets that contain points, lines and polygons. Using this schema user can differentiate between geometry primitives.

GMl format composed of geometry property and attribute detail in feature member tag. Here geometry defines which primitive is available in data. Whether it is point, line or polygon. These can be multilinestring or multipolygon. The given line of GMl defines the lineString, contains attributes as MED_DESCRI, RTT_DESCRI, F_CODE_DES, ISO and ISO_country in ogr tag. The srsName(Spatial reference system name) is given is file for each geometry as EPSG number.

LineString as Example of GML file Describing feature – Know Before performing shp to gml convert

<gml:featureMember>
 <ogr:India_raods fid="India_raods.0">
 <ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>77.8277403974823,35.5012774951288 77.8257523064861,35.4995003281321</gml:coordinates></gml:LineString></ogr:geometryProperty>
 <ogr:MED_DESCRI>Without Median</ogr:MED_DESCRI>
 <ogr:RTT_DESCRI>Secondary Route</ogr:RTT_DESCRI>
 <ogr:F_CODE_DES>Road</ogr:F_CODE_DES>
 <ogr:ISO>IND</ogr:ISO>
 <ogr:ISOCOUNTRY>INDIA</ogr:ISOCOUNTRY>
 <ogr:Length_km>0.30</ogr:Length_km>
 </ogr:India_raods>
 </gml:featureMember>

This also contains the bounding box of whole data with four corner latitude longitude.

<gml:boundedBy>
 <gml:Box>
 <gml:coord><gml:X>68.49822243392609</gml:X><gml:Y>7.92528433268866</gml:Y></gml:coord>
 <gml:coord><gml:X>97.33479210700925</gml:X><gml:Y>35.50128146412876</gml:Y></gml:coord>
 </gml:Box>
 </gml:boundedBy>

Convert shapefile Shp to GML (Geography Markup Language) using Ogr2Ogr –

For converting shapefile in GML format you need to have ogr2ogr utility in your system. If this is not available you can follow the given procedure.

Convert Shapefile shp to GML - Geography Markup Language
Convert Shapefile shp to GML – Geography Markup Language

After installing, you can check the availability of ogr2ogr in terminal by typing ogr2ogr, then you will be able to see the following result.

Convert- Shape file to GML

Command Line conversion – shp to gml-

Now, to convert Shapefile to GML you need to execute the following command-

Convert- Shape file to GML

In this command -f is the output file format i.e. GML then specify the output file name with .gml extension and after that input shapefile name with .shp extension.
Note- while executing this command you must check for .shx file, which contains the positional index for geometry objects.

Code- Convert shp to GML

If you want the conversion using programing or don’t want to use terminal then this can also be done using PHP. You can create a function that defined this command as string and can give this string in shell_exec() method to execute. Shell_exec() actually execute the command and returns output as string.

public function shptogml($shpfilepath,$output){
$query=”ogr2ogr -f GML $output.gml $shpfilepath”;
print_r($query);
shell_exec($query);
}

The GML output file will be something like this-

Convert Shapefile shp to GML - Geography Markup Language

Change coordinate system of GML file from shp-

Many of times, we need the output file in other coordinate system. For this we can use options of ogr2ogr utility. It has

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

So using these option, we can get the data in required coordinate system. So for this you can write the query as-

–> ogr2ogr -f GML -t_srs EPSG:32643 (new EPSG) -s_srs EPSG:4326 (old EPSG) newGmlFile.gml InputFile.shp

You also can visit Shapefile to GeoJSON, Shapefile to KML, Shapefile to TopoJSON, Shapfile to geopackage etc.

If you face any problem during implementing this tutorial, please let us know. Feel free to comment in given comment box.

SHP to GPX / GPS – Convert Shapefile to Global Positioning System

Shapefile shp can be converted into GPS ( Global Positioning System) using ogr2ogr utility. The GPS data is best option when routes and tracks are required in XML format. Here we will check how to convert shapefile shp to GPX / GPS.

Convert Shp to GPX – Using IGIS Map Tool

Go to MapOG Conversion Tool . Login with registered id and password or if you are new then register with valid email id. Then tap on Switch To button select conversion in the drop down list.

Upload your file from system or drive or from drop box. After uploading the file choose the output format. And in the last click on Convert File button. You will be directed to the map where converted file is published.

Download the converted SHP to GPX file from the download icon. For detailed conversion watch the video provided below.

Details for GPX/GPS (GPS Exchange Format) and Shapefile Format Before Converting Shapefile to GPX GPS data-

GPX (GPS Exchange Format) is a light-weight XML data format for the interchange of GPS data (waypoints, routes, and tracks) between applications and Web services on the Internet. Shapefile is collection of geometry with attributes mainly comprised of three files .shp for which stores geometry, .shx which stores index and .dbf which stores data of respective attributes.

The example is given below for linestring feature of gpx version. Here metadata contains the bounding box with min (lat,long) and max (lat, long). The tag trk defines the tracks, which contains extension tag, trkseg tag and trkpt tag. Here extension tag contains all the attributes in ogr tag with values. Trkseg tag is the track segment, which is composed of track points with latitude longitude.

<gpx version=”1.1″ creator=”GDAL 2.2.2″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:ogr=”http://osgeo.org/gdal” xmlns=”http://www.topografix.com/GPX/1/1″ xsi:schemaLocation=”http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd”>
<metadata><bounds minlat=”7.925284332688660″ minlon=”68.498222433926088″ maxlat=”35.501281464128759″ maxlon=”97.334792107009250″/></metadata>
<trk>
<extensions>
<ogr:MED_DESCRI>Without Median</ogr:MED_DESCRI>
<ogr:RTT_DESCRI>Secondary Route</ogr:RTT_DESCRI>
<ogr:F_CODE_DES>Road</ogr:F_CODE_DES>
<ogr:ISO>IND</ogr:ISO>
<ogr:ISOCOUNTRY>INDIA</ogr:ISOCOUNTRY>
<ogr:Length_km>0.3</ogr:Length_km>
</extensions>
<trkseg>
<trkpt lat=”35.501277495128768″ lon=”77.82774039748233″>
</trkpt>
<trkpt lat=”35.499500328132093″ lon=”77.825752306486052″>
</trkpt>
</trkseg>
</trk>

Convert- Shapefile shp to GPX / GPS- Global Positioning System

Convert shp to GPX /GPS using ogr2ogr utility-

Ogr2ogr utility has some options some of them are give below,

-dsco NAME=VALUE:

Dataset creation option (format specific)

-nlt type:

Define the geometry type for the created layer. One of NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON or MULTILINESTRING.

-f option:

format_name

GPX_USE_EXTENSIONS:

By default, the GPX driver will discard attribute fields that do not match the GPX XML definition (name, cmt, etc.).

If GPX_USE_EXTENSIONS=YES is specified, extra fields will be written inside the <extensions> tag.

–> ogr2ogr -f “GPX” -dsco GPX_USE_EXTENSIONS=YES output_gpx.gpx India_raods.shp -nlt MULTILINESTRING

If you don’t have ogr2ogr utility in your system please execute the given commands to install ogr2ogr,

Convert- shp to gpx

Check the presence of utility by typing ogr2ogr in command prompt-

Convert- shp to gpx

Code to Convert GPX GPS from Shp

The conversion thing can be done in PHP using shell_exec() method, given below-

public function shptogpx ($shpfilepath,$output,$type) {
$query=”ogr2ogr -f GPX -dsco GPX_USE_EXTENSIONS=YES $output.gps $shpfilepath.shp -nlt $type”;
shell_exec($query);
}

Convert- shp to gpx

Change coordinate system of GPS/GPX file-

Changing coordinate system using ogr2ogr is a one of the simple task. It has various option, here two are given, which will help to convert the coordinate system into desired one.

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

So using these option, we can get the data in required coordinate system. So for this you can write the query as-

–> ogr2ogr -f GPX -t_srs EPSG:32643 (new EPSG) -s_srs EPSG:4326 (old EPSG) -dsco GPX_USE_EXTENSIONS=YES newGPX_File.gpx  InputFile.shp -nlt MULTILINESTRING

You also can visit Shapefile to GeoJSON, Shapefile to KML, Shapefile to TopoJSON, Shapfile to geopackage, Shapefile to MapInfoFile etc.

If you find any problem during implementing this tutorial, please let us know. Feel free to comment in given comment box.

Convert Shapefile to GeoPackage – GPKG

GeoPackage is an open, standards-based, platform-independent, portable, self-describing, compact format for transferring geospatial information. Whereas shapefile is collection of geometry with attribute detail. Here in this article, we will check how to convert Shapefile to GeoPackage or GPKG GIS format.

Online Tool To Convert Shapefile to GeoPackage file

The GeoPackage Encoding Standard describes a set of conventions for storing the following within an SQLite database:

1. Vector features
2. Tile matrix sets of imagery and raster maps at various scales
3. Attributes (non-spatial data)
4. Extension

Convert Shapefile to GeoPackage - GPKG
Convert Shapefile to GeoPackage – GPKG

You can open the gpkg (GeoPackage) file in QGIS software and see some of the general properties of gpkg (GeoPackage) file-

1. Storage type of this layer
–> GPKG
2. Description of this provider
–> OGR data provider (compiled against GDAL/OGR library version 2.2.2, running against GDAL/OGR library version 2.2.2)
3. Source for this layer
–> /var/www/html/PHP_pgrouting/public/outputllll.gpkg
4. Geometry type of the features in this layer
–> Line
5. The number of features in this layer
–> 19148
6. Capabilities of this layer
–> Add Features, Delete Features, Change Attribute Values, Add Attributes, Delete Attributes, Fast Access to Features at ID, Change Geometries, Presimplify Geometries, Presimplify Geometries with Validity Check
7. Extents
–> In layer spatial reference system units
xMin,yMin 68.4982,7.92528 : xMax,yMax 97.3348,35.5013
8. Layer Spatial Reference System
–> +proj=longlat +datum=WGS84 +no_defs

Convert Shapefile to GeoPackage – GPKG  using OGR2OGR-

The command has some options, which can be seen by typing ogr2ogr in terminal. If this gives you error that means you don’t have ogr2ogr utility in your system. To install that you can follow the statements given below-

If Ogr2ogr utility is available, you will see the following result.

The command is given as-

–> ogr2ogr -f GPKG outp_gpkg.gpkg India_raods.shp

The command contains Utility name, option for file format, name for format, output file with .gpkg extension and input shape file.

To install ogr2ogr utility-

Convert Shapefile to GeoPackage - GPKG
Convert Shapefile to GeoPackage – GPKG

To check  if ogr2ogr utility is working properly or not-

Convert- Shapefile to GPKG- GeoPackage

Online Tool To Convert Shapefile to GeoPackage file

PHP Code to Convert Shapefile to GeoPackage – GPKG-

Using shell_exec() method, you can run the command in PHP. This return output as string.

public function shptogpkg($shpfilepath,$output) {
$query=”ogr2ogr -f GPKG $output.gpkg $shpfilepath.shp”;
shell_exec($query);
}

The output file can be open in QGIS software. I have converted Road File of one such country. Here is the output look in QGIS tool after Converting Shapefile to GeoPackage.

Convert Shapefile to GeoPackage - GPKG

Change Coordinate system of Output-

Many of times, we need the output file in other coordinate system. For this we can use options of ogr2ogr utility. It has

-a_srs srs_def:

Assign an output SRS

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

So using these option we can get the data in required coordinate system. So for this you can write the query as-

—> ogr2ogr -f GPKG -s_srs EPSG:4326 -t_srs EPSG:32643 Output_file_name.gpkg Input_file_name.shp

Here define old EPSG (European Petroleum Survey Group) in -s_srs option and new in -t_srs option.

You may also Check Shapefile to GeoJSON, Shapefile to KML, Shapefile to TopoJSON, etc conversion.

Let me know if you find any problem in converting Shapefile to GeoPackage file, by commenting below in the box provided.

Earthquake Map – Create EarthQuake Map GIS

An earthquake (also known as a quake, tremor or temblor) is the shaking of the surface of the Earth, resulting from the sudden release of energy in the Earth’s lithosphere that creates seismic waves. Earthquakes can range in size from those that are so weak that they cannot be felt to those violent enough to toss people around and destroy whole cities. The seismic or seismic activity of an area refers to the frequency, type and size of earthquakes experienced over a period of time.

Earthquake is a nature’s most unpredictable and one of her most devastating natural disasters. When high intensity earthquakes strike they can cause thousands of deaths and billions of dollars in damaged property. For decades, experts have studied major earthquakes; most have focused on fatalities and destruction in terms of the primary effects, the shaking unleashed.
At the Earth’s surface, earthquakes manifest themselves by shaking and sometimes displacement of the ground. When the epicenter of a large earthquake is located offshore, the seabed may be displaced sufficiently to cause a tsunami. Earthquakes can also trigger landslides, and occasionally volcanic activity.

Worldwide, at least 133 earthquakes occurred in 2011, during which people died, were injured or lost their homes or which caused immense damage to property.
The intensity value of earthquake is divided in 9 parts as given below

Earthquake Map

About Demo-

Earthquake information of Padang, Indonesia is shown using raster Geo-tiff file. Padang is the capital of the province of West Sumatra in Indonesia. The intensity of earthquake on map can be represent by color variation from dark to light shade. Here dark shade shows high shocks and light color medium and low shocks. These intensity value can be extract from layer using Grey band information.

Data Preparation:

This demo shows the raster overlay on map with the help of Leaflet JavaScript library. Here raster data is in Geo Tiff format and is published on Geo server. For this demo we have downloaded data from http://geonode.inasafe.org/layers/geonode:padang_eq_2009_wgs84 website. It is a raster data containing single Grey band. In this demo we render raster data on map and shown the value of earthquake intensity using pop-up on marker.

Raster data on GeoServer and styling:

GeoServer is an open source server for sharing geospatial data in both vector and raster format. It is designed to host major data sources, which can easily render on maps.
To Download and install GeoServer please follow the last article. Then login to the GeoServer and publish data on the Geoserver and get the link from layer preview section. You can visit the articles to know how to publish and style the GeoTiff file on Geoserver.

Render Raster on Map:

Before rendering tiles on map, you need to create a division in html that will contain map. You also can set its view by providing center latitude and longitude with zoom level.

var map = L.map(‘map’).setView([-1.03705, 99.64336],8);
var searchLayer ;
L.tileLayer(‘http://{s}.tile.osm.org/{z}/{x}/{y}.png’, {
attribution: ‘&copy; <a href=”http://osm.org/copyright”>OpenStreetMap</a> contributors’
}).addTo(map);

To render this data on map, we need to get the data from GeoServer, which can be done by using leaflet javascript library. In the Leaflet library we have Leaflet-WMS plugin.
What is WMS service: WMS (web map service), is a way of publishing maps. This format is similar to map tiles. A WMS image is defined by the coordinates.

For styling purpose we have used geoserver styling option, where you can style your data according to your attributes or bands etc.

NamedLayer – name of the layer.

<FeatureTypeStyle>
<Rule>
<Name> RUle style </Name>
<Title>Opaque Raster</Title>
<Abstract>A raster with 100% opacity</Abstract>
<RasterSymbolizer>
<Opacity>1.0</Opacity>
<ChannelSelection>
<GrayChannel>
<SourceChannelName>1</SourceChannelName>
</GrayChannel>
</ChannelSelection>
<ColorMap extended=”true”>
<ColorMapEntry color=”#00FA08″ quantity=”0″/>
<ColorMapEntry color=”#F6FA00″ quantity=”2″/>
<ColorMapEntry color=”#FA9F00″ quantity=”3″/>
<ColorMapEntry color=”#FA4C00″ quantity=”6″ />
<ColorMapEntry color=”#A21612″ quantity=”9″/>
</ColorMap>
</RasterSymbolizer>
</Rule>
</FeatureTypeStyle>

L.tileLayer.wms:

It provides a simple interface for loading tiles from a WMS service. This facility is given in Leaflet javascript library we just need to add its plugin. Here we also need to provide layer name, format of layer and make transparent as true.

var raster = L.tileLayer.wms(‘http://139.59.42.235:8080/geoserver/portfolio/wms?’, {
layers: ‘padang_eq_2009_wgs84’,
format: ‘image/png’,
transparent: true
}).addTo(map);

Intensity of earthquake is denoted by color ramp. To know the value of earthquake intensity at particular location, we can place the marker and can read the popup. Delete button is given to delete all markers.

function showGetFeatureInfo(latlng, layer){
var prop = layer.features[0].properties;
var mark=L.marker([latlng.lat,latlng.lng]).addTo(map);
var gray=prop[‘GRAY_INDEX’];
mark.bindPopup(“Earthquake Value: “+gray.toString());
}

Convert Shapefile to CSV

Sometimes shapefile becomes bulky to carry from one place to another. As it contains various files as .shp, .prj, .shx, .dbf etc. On the other side CSV contains every detail in one file. So CSV can be considered as a good option when you don’t need to render data on map as polygon or polyline or point. There are various options to create CSV from a shapefile either offline or online.

CSV is comma separated value file which is one of the alternative method for storing spatial data in spreadsheet format. You can also upload the Excel file and convert it into spatial data via QGIS tool. There are also other online tools available which can directly convert the Shapefile to CSV. We will show you how easy and useful is IGISMAP for the conversion process. Following are the methods to convert Shapefile to CSV using IGISMAP Converter tool.

IGISMAP (now MAPOG) to Convert Shapefile to CSV

Go to MapOG Converter Tool Shapefile to CSV, after logging in with your registered email and password. If you are a new user, click the Sign Up button in the Login popup and register to IGISMAP by filling the details.

There are three main steps for using GIS Converter:

  • Upload the data
  • Choose the format to which it should be converted
  • Download the converted file.

Step one is to upload your Shapefile which you want to convert. You can upload the file from your system or select from the Recent Files.

Converter Tool - Upload Shapefile
Upload Shapefile

Here we using the KML file of California state boundary.

Step two is to select choose the output format of the converted file, in this case its CSV. You can also set the Coordinate Reference System of your preference. As a default CRS will set to WGS 84 (World) [EPSG:4326]. Click on the Convert File button.

Converter Tool - CSV as Output Format
Select CSV as Output Format

Your Shapefile will then get converted to CSV file after a few seconds and will be published in the map canvas. You can download the CSV file of California state boundary by clicking the Download Converted File button.

Download and Publish CSV File
Download and Publish CSV File

The output CSV file is generated with location information represented in WKT format under the field named ‘WKT’.

You can also choose to style the layer or continue with further conversion process by clicking the Convert Another File button.

Shapefile to CSV conversion – OFF-Line (command line) Tool

For using off-line command in your system you need to install ogr2ogr. There is very simple command to convert shapefile to csv file if you dont want to install a bulky GIS tool like QGIS or ArcGIS.

I am giving a way to install GDAL on ubuntu, while the process is almost similar to install the GDAL tool in Windows, Mac or other Linux system.

For installing GDAL for Ubuntu please follow the given commands.

Shapefile to CSV conversion
Shapefile to CSV conversion

After installing you can check the availability of ogr2ogr by typing ogr2ogr on terminal.

Shapefile to CSV conversion

After that you can follow the command to convert the shapefile to csv (comma separated value format file)

–> ogr2ogr -f CSV output_file_name.csv input_shp_name.shp -lco GEOMETRY=AS_WKT

The same work can be done in code-igniter or using PHP. For that you need to execute this command in shell_exec() method, which takes the command in string format and returns the output in string.

–>  $shpToCSV=”ogr2ogr -f CSV $out.csv $shp.shp -lco GEOMETRY=AS_WKT”;
–>  shell_exec($shpToCSV);

These statements are written in PHP. $ signifies the variable. Here input and output is taken from user and command is written in double quotes to convert in string. Then it is given to shell_exec() method.

Shapefile to CSV conversion – With QGIS Open source Software

If you don’t need to install any library or run online tools as you already have installed GIS software. You can convert shapefile in CSV using GIS software also. You just need to open the shapefile as vector layer and save it as CSV. For adding the vector layer tyou need to go to Layer->add vector layer and browse the shapefile. After the right click on shapefile title and save it as csv. you can also save some selected part.

Shapefile to CSV conversion

You may also look on other Conversion of Files like Shapefile to KML or Shapefile to GeoJSON or GeoJSON to TopoJSON etc.

In this way you can convert shapefile in CSV format. Hope you find tutorial of shapefile to CSV conversion is helpful for you. If you find any problem in converting GIS data in CSV format, do comment below with the problem statement. Also if you know any other tool for converting shapefile, do provide your suggestion in the provided box below.

Split Sub Divide polygon layer using QGIS – Shapefile, kml, GeoJson

Sub divide or split polygon in multiple parts. QGIS is a special spatial tool, which easily operate with GIS data files such as shapefile, kml or geojson. You might find many cases, where you need to split up polygon or diivde polygon in different parts. For instance, lets suppose we have  administrative boundary shapefile of US and have an image file of sub administrative part, and we need an output shapefile of sub administrative part. For making this possible we need to divide or split up the polygon with the tool.  Here in this article, we are splitting up the existing polygon shapefile data file.

You can also try Online Split Tool To split the polygon in equal parts with MAPOG

Pre Requisite for Split Sub Divide polygon layer using QGIS

I will use digitizing tool plug-in to split the polygon into multiple polygons. I have already written about digitize in QGIS, but to be specific to split of polygon this article would demonstrate you in more detail. Following are the pre requirement to perform split operation:

1.) QGIS Software as tool

2.) Polygon Shapefile as Data

3.) Installed plugin Digitizing tools : This plug-in can be install by navigate yourself to plug-in tab->Manage and install plug-in->Digitizing Tool->install.

Split or Sub Divide polygon layer using QGIS

Split Sub Divide polygon layer using QGIS : Steps

For example we need to subdivide the below polygon in two different polygons:

Split or Sub Divide polygon layer using QGIS
Split or Sub Divide polygon layer using QGIS

To achieve our target we need to implement following steps:

1. Add your polygon shape file to canvas.

2. For spliting up polygon we first need to convert polygon shapefile to polyline shapefile. To convert polygon to polyline, QGIS provides a direct way to do the same. Just select Vector tab from the menu, and navigate to Geometry tools options and Select Polygon to line, as shown in figure:

Split or Sub Divide polygon layer using QGIS

Select the polygon layer and run this tool. the output layer will be line Shapefile as shown below.

Split or Sub Divide polygon layer using QGIS

3. Now we need to Edit the output line shapefile.  This can be done by enabling editing option. Then draw the line in the way you want to split your polygon data.

Split or Sub Divide polygon layer using QGIS
Split or Sub Divide polygon layer using QGIS

At the time of adding lines you need to join vertices for proper topology for that you can use the snapping tool.

Split or Sub Divide polygon layer using QGIS

4. Now line and polygon files are available. Now turn on both layers in canvas and select polygon shape file, then the line. Then enable editing for polygon shapefile you will see the digitizing tools in toolbar.

Split or Sub Divide polygon layer using QGIS
Split or Sub Divide polygon layer using QGIS

After that select “Split selected feature with selected line from another layer” tool. Note that the polygon and line both should be selected to run this tool.

Split or Sub Divide polygon layer using QGIS

5. Repeat this step to make more divisions. The output file would be as shown below

Split or Sub Divide polygon layer using QGIS
Split or Sub Divide polygon layer using QGIS

7. Now correct attribute table as all the split polygons will have same entries in attribute table.

In this way you can split the polygon into multiple polygon entity. If you find any problem in Split Sub Divide polygon – Shapefile, kml, GeoJson data, then do comment below with the problem statement, so that we can look for solution together. Or if you know any other method to split the polygon do let us know by commenting below in the box provided.