PHPackages                             andriell/osm2sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. andriell/osm2sql

ActiveLibrary[Database &amp; ORM](/categories/database)

andriell/osm2sql
================

Library to import OSM (open street map) XML file into SQL database

v2.0.9(6y ago)32.4k1BSD-3-ClausePHPPHP &gt;=5.3.0

Since Sep 15Pushed 6y ago1 watchersCompare

[ Source](https://github.com/andriell/php-osm-to-sql)[ Packagist](https://packagist.org/packages/andriell/osm2sql)[ Docs](https://github.com/andriell/php-osm-to-sql)[ RSS](/packages/andriell-osm2sql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (13)Used By (0)

Convert OSM XML file to SQL for MySQL
=====================================

[](#convert-osm-xml-file-to-sql-for-mysql)

Quick start
-----------

[](#quick-start)

Download OSM XML here

### Example 1

[](#example-1)

Convert OSM to SQL file

1. Create database tables from file resources/mysql.sql
2. Convert OSM to SQL inserts

    ```
     ini_set('memory_limit', '10M');
     // This class read big osm file
     $largeXmlReader = new LargeXmlReader();
     $largeXmlReader->setFilePath('/path/to/very/big/osm/xml/file.osm');

     // This class listens reader class and write data into file
     $listener = new FileReaderListener('/path/to/new/sql/file.sql');

     // Insert 500 rows in one operation
     $listener->setInsertSize(500);
     // Use INSERT IGNORE ...
     $listener->setInsertIgnore(true);
     $largeXmlReader->setListener($listener);
     $largeXmlReader->setProgressListener(function($readSize, $totalSize) use ($progress) {
         echo 'Completed ' . $readSize . ' Mb of ' . $totalSize . " Mb\n";
     });
     // Start parsing
     $largeXmlReader->parse();

    ```
3. Import sql file in to database

    ```
     mysql -u username -p database_name < /path/to/new/sql/file.sql

    ```

### Example 2

[](#example-2)

You can create your ReaderListener and write data directly to the database

```
class DbReaderListener extends AbstractReaderListener
{
    // ...
    protected function write($table, $sql)
    {
        $this->myDbConnection->query($sql);
    }
}

// ...
$largeXmlReader->setListener(new DbReaderListener());
// ...

```

### Example 3

[](#example-3)

Write data directly to the database and calculate building, highway, place tables

```
ini_set('memory_limit', '100M');

include 'vendor/autoload.php';

$file = '/path/to/very/big/osm/xml/file.osm';

// This class read big osm file
$largeXmlReader = new \osm2sql\LargeXmlReader();

// This class listens reader class and write data into database
$listener = new \osm2sql\mysql\PdoDbBuilder('mysql:host=localhost;dbname=osm;port=3306', 'user', 'password');
// Insert 100 rows in one operation
$listener->setInsertSize(100);
// Use INSERT IGNORE ...
$listener->setInsertIgnore(true);
$listener->setProgressListener(function ($size, $total) {
    echo 'Update DB ' . $size . ' row of ' . $total . " rows\n";
});
$listener->setExceptionListener(function($e, $sqlStr) {
    echo $sqlStr . "\n";
    echo $e . "\n";
});
// Delete all relations, way, node in database
$listener->deleteRelation();
$listener->deleteWay();
$listener->deleteNode();

$largeXmlReader->setFilePath($file);
$largeXmlReader->setListener($listener);
$largeXmlReader->setProgressListener(function($readSize, $totalSize) {
    echo  'Read file ' . $readSize . ' Mb of ' . $totalSize . " Mb\n";
});
// Insert new relations, way, node from osm file to database
$largeXmlReader->parse();

// Clear and calculate highway, building, place
$listener->deleteHighway();
$listener->calculateHighway();
$listener->deleteBuilding();
$listener->calculateBuilding();
$listener->deletePlace();
$listener->calculatePlace();

echo 'Done';

```

You can create your DbBuilder class and use database connection in your framework

```
class MyDbBuilder extends \osm2sql\mysql\AbstractDbBuilder
{
    // ...
    protected function querySelect($sqlStr)
    {
        $rows = $this->myDbConnection->query($sqlStr);
        $r = [];
        foreach ($rows as $row) {
            $r[] = (array) $row;
        }
        return $r;
    }

    protected function queryUpdate($sqlStr)
    {
        return (int) $this->myDbConnection->exec($sqlStr);
    }
}

```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~61 days

Recently: every ~137 days

Total

12

Last Release

2481d ago

Major Versions

v1.0.2 → v2.0.02018-01-19

PHP version history (2 changes)v1.0.0PHP &gt;=5.3.0

v2.1.0.x-devPHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/284e5878cad7573891407697c19fafd47dd403ed7fbd323927291e1915cbdf01?d=identicon)[andriell](/maintainers/andriell)

---

Top Contributors

[![andriell](https://avatars.githubusercontent.com/u/7204224?v=4)](https://github.com/andriell "andriell (72 commits)")

---

Tags

importosmOpenStreetMap

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andriell-osm2sql/health.svg)

```
[![Health](https://phpackages.com/badges/andriell-osm2sql/health.svg)](https://phpackages.com/packages/andriell-osm2sql)
```

###  Alternatives

[ddeboer/data-import

Import data from, and export data to, a range of file formats and media

5604.3M9](/packages/ddeboer-data-import)[portphp/portphp

Data import/export workflow

2702.9M22](/packages/portphp-portphp)[rah/danpu

Zero-dependency MySQL dump library for easily exporting and importing databases

64401.8k10](/packages/rah-danpu)[cyber-duck/laravel-excel

This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection.

74225.0k](/packages/cyber-duck-laravel-excel)[in2code/migration

Framework for any kind of TYPO3 migrations and imports. Also exports and imports content from and to json files.

6277.0k3](/packages/in2code-migration)[code-rhapsodie/dataflow-bundle

Data processing framework inspired by PortPHP

1852.8k3](/packages/code-rhapsodie-dataflow-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
