Tag Archives: script

Exporting high resolution image from QGIS project

qgis_hasil_resolusi

Problem:

You want to export your QGIS project into high resolution image. QGIS desktop “Project” –> “Save as Image” only save/export the project into a low resolution image (i.e. your desktop resolution).

qgis_banyumanik

Solution:

1. Finish your project in QGIS

Finish your GIS project in QGIS Desktop, choose which layers to be shown, what style should it use, etc.

2. Choose your extent that will be exported into image

For myself, I choose the full-extent, but you may choose the extent as you wish. You my use “Vector” –> “Coordinate Capture” to help copying the coordinates. You will need to write down the coordinates for [xMin, yMin] (top-left coordinate) and for [xMax, yMax] (bottom-right coordinate). Save and close your QGIS project.

3. Execute the export command via command line of QGIS

Open your command prompt/console (preferably as Administrator/root). Go to your QGIS binary folder, where you install QGIS +”bin” folder, for example C:\Program Files\QGIS Brighton\bin. Use this command template:

qgis --project "c:\where\you\save\your\qgis\project\file.qgs" --snapshot "c:\where\you\want\to\save\your\image.png" --height result_image_height --width result_image_width --extent xmin,ymin,xmax,ymax

For example:

C:\Program Files\QGIS Brighton\bin>qgis --project "D:\work\dita s1\pmapper\Semarang\Kec_Banyumanik\banyumanik.qgs" --snapshot "c:\users\achmadz\documents\padangsari.png" --height 5000 --width 5000 --extent 435825.86153,9218725.47861,438150.22840,9217277.65534

That command will open QGIS Desktop for a moment then close itself. The command will produce padangsari.png in c:\users\achmadz\documents\. Open the image, and you’ll see the image in high resolution (5000×5000 pixels).

qgis_hasil_resolusi

Using linkbucks API / posting data using cURL in PHP

Background:

Solution:

  • Register to linkbucks if you don’t have any account there (it’s free and you probably should use email other than gmail and hotmail if you want quick confirmation from linkbucks)
  • Find your username and API password from your profile page: https://www.linkbucks.com/Profile/ . If your API password is empty, just click “Reset API Password” link to show it.

This is our little PHP function that help us to POST data to certain URL:

//function to POST data using cURL in PHP
function curl_post($url, $data, $ip="", $ref="", $ua="") {
    //initiate cURL
    $ch=curl_init($url);
    
    //set referer only if stated by user
    if ($ref!="") {
        curl_setopt($ch, CURLOPT_REFERER, $ref);
    }
    
    //set IP interface if needed (for multi-IP server)
    if ($ip!="") {
        curl_setopt($ch, CURLOPT_INTERFACE, $ip);
    }
    
    //set user agent if stated by user
    if ($ua!="") {
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    }
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response=curl_exec($ch);
    return $response;
}

How to use it to call linkbucks API:

$data_kirim=array(
    'user'=>'chmdznr',  //my username
    'apiPassword'=>'d55ae4eaf97b445f',  //my API password
    "originalLink"=>"http://google.com",  //the URL that need to be shorted
    "adType"=>2,  //paid links
    "contentType"=>1,   //not an adult content
    "domain"=>"yyv.co"  //target domain, a lot of possibility here, the default is: linkbucks.com 
);

//call linkbucks API
$url='https://www.linkbucks.com/api/createLink/single';

$returned_data=curl_post($url, json_encode($data_kirim));
$parsed_data=json_decode($returned_data);

//simple error checking
if (!empty($parsed_data->link)) {
    echo "This is your short URL : ".$parsed_data->link;
} else {
    echo "Something wrong! ".$parsed_data->errorDescription;
}

The curl_post function is quite generic, you can use it to send data to remote URL using POST method like this:

$login_data=array(
   'username'=>'admin',
   'password'=>'admin'
);

$target_url="http://the_login_website.com/";
$returned_data=curl_post($target_url, http_build_query($login_data));

You should notice that the data array need to be converted using http_build_query first (in linkbucks’ case, it doesn’t need to be converted first).
You can also add more advanced feature in the function such as handling the timeout and cookie.

[Example] Solving math linear equations with 4 unknown variables

We’ll put some use of Matrix library for CodeIgniter that we already implemented to solve a linear equation.
Let’s say we have linear equations with 4 unknown variables like this:
$latex \begin{array}{rcl}
2a+3b+c+6d &=& 4 \\
4a-2b+0.5c+d &=&2 \\
6a+b-d&=&-1 \\
2a+2b-c+3d&=&4 \end{array}$

That linear equation can be written in Matrix notation like this:
$latex \left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right] \left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right] $

And can be solved by this matrix equation:
$latex \left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right]^{-1} \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right] $

And this is how we solve it on php/CodeIgniter:

// load the phpmath library
require_once(APPPATH."/third_party/phpmath/Matrix.php");

//building the 'left' matrix
$left=array(
    array(2,3,1,6),
    array(4, -2, 0.5, 1),
    array(6,1,0,-1),
    array(2,2,-1,3),
);
// left matrix class
$mLeft=new Matrix($left);

//building the 'right' matrix
$right=array(
    array(4),
    array(2),
    array(-1),
    array(4),
);
// right matrix class
$mRight=new Matrix($right);

// calculate inverse of left matrix
$inv_mLeft=$mLeft->inverse();

// find the equation solution by multiplying the inverse of left matrix and the right matrix
$solution=$inv_mLeft->times($mRight);

// $solution is the value of a,b,c and d variables. Let's dump it as an array!
var_dump($solution->getArray());

Quite simple isn’t it?!

PS.: I’m glad I can use my Latex knowledge in this post 😛

Matrix operation library for CodeIgniter

This tips is useful if you someday handling web-based project that involving Matrix handling and you want to implement it using CodeIgniter. We’ll use PHPMath library for this.

  • Download the latest version of phpmath library from here: http://www.phpmath.com/build02/downloads/
  • Open the archive (build02.tar.gz) and see the content, you’ll see “build02” folder, go inside, you’ll see more folders there:
phpmath archive content

phpmath archive content

  • Because we only need the matrix handling and operation library, we just need the “JAMA” folder inside.
  • Extract this “JAMA” folder into your CodeIgniter’s application/third_party/ library, rename it to more meaningful folder, I prefer “phpmath”. Your third_party library will be look like this:
phpmath inside third_party folder

phpmath inside third_party folder

  • Go inside “phpmath” folder, you’ll see more folders there. We don’t need “docs”, “examples” and “tests” folder, we can delete them, your phpmath folder will be like this:
phpmath after deleting unnecessary folders

phpmath after deleting unnecessary folders

And you are done! (In this tips: not like mPDF and PHPExcel library, we don’t need to create a new file/class in the application/libraries folder)

How to use it?

Quite simple, check and test these code samples inside your controller:

 
// load the phpmath library
require_once(APPPATH."/third_party/phpmath/Matrix.php");

// define new matrix
$raw_matrix_a=array(
     array(1,2,3,4),
     array(5,6,7,8)
);
$raw_matrix_b=array(
     array(1,2),
     array(3,4),
     array(5,6),
     array(7,8)
);
$raw_matrix_c=array(
     array(9, 10),
     array(11, 12)
);
$raw_matrix_d=array(
     array(13, 14),
     array(15, 16)
);
// build the Matrix class
$mA=new Matrix($raw_matrix_a);
$mB=new Matrix($raw_matrix_b);
$mC=new Matrix($raw_matrix_c);
$mD=new Matrix($raw_matrix_d);

//get the matrix element
var_dump($mA->get(1,1));
//set value
$mA->set(1,1, 0);
//get matrix dimension
var_dump($mA->getRowDimension());
var_dump($mA->getColumnDimension());

//addition
$mE=$mD->plus($mC); //must have same dimension
//subtraction
$mE=$mD->minus($mC);
//multiplication
$mE=$mA->times($mB);
//inverse
$mE=$mD->inverse(); // must be square matrix
//transpose
$mE=$mB->transpose();

Easily integrate mPDF into CodeIgniter Framework

Got the tip from here: http://davidsimpson.me/2013/05/19/using-mpdf-with-codeigniter/
On this previous post, I tell you on how to easily integrate ezPDF into CodeIgniter but the problem is: the PDF generation is not template-based. So, you have to write a lot of codes just to generate simple PDF output.
By integrating mPDF into your CodeIgniter-based site, you can create a PDF output via templating system just as simple as loading a view and pass through some variables into it. mPDF also support CSS, so, you can style your HTML template as cool as you wish.
Here’s the steps-by-steps guide:

  • Download mPDF from http://www.mpdf1.com/mpdf/index.php?page=Download . Remember to download the “Full installation” first (a bit big) and then you can download the patch/upgrade (a little smaller size)
  • Extract it to your application/third_party/ folder of your CodeIgniter. Your third_party folder would be like this:

    mPDF in third_party folder

    mPDF in third_party folder

  • Create a new file in your application/libraries/ name it m_pdf.php and these are the contents:

  • Your library folder would be like this:
    mPDF in the library folder

    mPDF in the libraries folder

  • And it's done!

How to use it?
Create a CodeIgniter "view file", let's named it pdf_output.php:

load->view('header'); //let's assume that we already have 'header' view file
?>
Hello, this just a simple HTML template.
I'm a variable passed from the controller, my content is
load->view('footer'); //let's assume that we already have 'footer' view file
?>

Now, inside the controller, you can do this:

//this data will be passed on to the view
$data['the_content']='mPDF and CodeIgniter are cool!';

//load the view, pass the variable and do not show it but "save" the output into $html variable
$html=$this->load->view('pdf_output', $data, true); 

//this the the PDF filename that user will get to download
$pdfFilePath = "the_pdf_output.pdf";

//load mPDF library
$this->load->library('m_pdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->load();
//generate the PDF!
$pdf->WriteHTML($html);
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "D");