venerdì 8 agosto 2014

How search/and/replace in a file

The need is to change the paths in a text file to adapt the filepaths from a pc to another pc.

To do this the choose is to use the "sed"  command.

After a googling I find this hint:
http://stackoverflow.com/questions/525592/find-and-replace-inside-a-text-file-from-a-bash-command
In it there is this sample:
in the file "/tmp/file.txt" change all occurrence of "abc" to the new string "XYZ"

The start point is this hint

sed -i -e 's/abc/XYZ/g' /tmp/file.txt
 
from here

qgis-server: remove maptip virtual field added to a getfeatureinfo response.

#1:

QGIS-Server add a ridicolous "maptip" attribute to some response of GetFeatureInfo request.
Is quite impossile to understand why it add it.  I almost was not capable to undestand why.
:))
 
I try also to undesrad why some layer yes and some other layer no, but was not capable to undestand this.
I try to ask to the ML of qgis (user and dev), but no response to this requests.
And this is the more clear explanation of what is this MapTIP virtual field added.

SO , I guess the strategy to add that field is quite wrong, infact often add it when not requested.

Perhaps it is useful for the specific qgis-server client, but absolutely unuseful for a standard WMS client.
So the better and rapid solution,
Is remove it and recompile qgis.


To do this:
-----------------------------------------------------------------
Open
QuantumGIS/src/mapserver/qgswmsserver.cpp

around line 1812

comment all this code:

      //add maptip attribute based on html/expression (in case there is no maptip attribute)
//      if ( layer->fieldNameIndex( layer->displayField() ) < 0 )
//      {
//        QString displayField = layer->displayField();
//        if ( !displayField.isEmpty() )
//        {
//          QDomElement maptipElem = infoDocument.createElement( "Attribute" );
//          maptipElem.setAttribute( "name", "maptip" );
//          maptipElem.setAttribute( "value",  QgsExpression::replaceExpressionText( displayField, &feature, layer ) );
//          featureElement.appendChild( maptipElem );
//        }
//      }

Ok, after this, go around line 2906 and comment also this code:

  //add maptip attribute based on html/expression (in case there is no maptip attribute)
//  if ( layer->fieldNameIndex( layer->displayField() ) < 0 )
//  {
//    QString displayField = layer->displayField();
//    if ( !displayField.isEmpty() )
//    {
//      QString fieldTextString = QgsExpression::replaceExpressionText( displayField, feat, layer );
//      QDomElement fieldElem = doc.createElement( "qgs:maptip" );
//      QDomText maptipText = doc.createTextNode( fieldTextString );
//      fieldElem.appendChild( maptipText );
//      typeNameElement.appendChild( fieldElem );
//    }
//  }

And after this you can recompile the qgi happy that the stupid maptip field is no more in the

Et-Voila'.
.

giovedì 7 agosto 2014

How retrieve polygons from a set of lines

The use-case is that the usual  BuildArea don't work because the linea are tto mixed and intersecating each other.

The solution is to noed them before the BuildArea.

So, as example:
this code:

The shapefile with the lines to transform in polygon is named "errbordo.shp"
----------------
.headers on
.timer on

.loadshp errbordo errbordo CP1252 3003 geometry pk_uid auto 2d 1

create table tabella_nodata as select * from errbordo;
select RecoverGeometryColumn('tabella_nodata','geometry',3003,'LINESTRING','XY');
select CreateSpatialIndex( 'tabella_nodata','geometry' );
select AddGeometryColumn( 'tabella_nodata','geometry_noded', 3003, 'MULTILINESTRING','XY' );
select AddGeometryColumn( 'tabella_nodata','geometry_poligono', 3003, 'MULTIPOLYGON','XY' );

update tabella_nodata set geometry_noded = ST_NODE(geometry);

update tabella_nodata set geometry_poligono = ST_Multi(ST_BuildArea(geometry_noded));

the field "geometry_poligono" has the polygon.
----------------

The End.


lunedì 14 luglio 2014

Calculate the mapping between PIXELS and MM in a outline rendering

The problem was:
In qgis the outline was rendered with a 0.26 mm outline.

How much it is in pixels ?

The formulaes are these:

1 meters = 39.37007 inch

1 mm = 39.37007 / 1000 = 0.03937007 inch

0.26 mm = ( 39.37007 / 1000 ) * 0.26mm (the outline)  = 0.01023

If the monitor is at 72dpi:
 72dpi => 0.01023 x 72 = 0.73 pixel width

If the monitor is at 91dpi:
91dpi => 0.01023 x 91 = 0.93 pixel width

Thats all.

mercoledì 18 giugno 2014

How remove a kind of file recursively on Linux

I need to remove all the *.tiff files from a set of folders recursively.
The  most obvious solution
rm -Rf *.tiff
don't work.
:)

This is the solution I find:

find . -name '*.tiff' -exec rm {} \;


giovedì 12 giugno 2014

Using GDAL to add a field to a shapefile and populate it

The need is:

I have a set of shapefile and need to have another copy of them addind a new field and populating it with the filename (without extension it match a code identifier).


The solution was using a shell dos and the ogr2ogr and ogrinfo utility:

This is the code:

I use the gdal batch utility from osgeo4w package.

-----
mkdir destination-folder
for /R D:\temp\temp2\input-folder %%f IN (*.shp) do (
    ogr2ogr D:\temp\temp2\destination-folder\zz_%%~nf_output.shp %%f
    cd D:\temp\temp2\input-folder
    ogrinfo zz_%%~nf_output.shp -sql "ALTER TABLE zz_%%~nf_output ADD COLUMN filename character(128)"
    ogrinfo zz_%%~nf_output.shp -dialect SQLITE -sql "UPDATE zz_%%~nf_output SET filename = '%%~nf'"
    cd ..
)
------

Just an explain of some details:
In the ogrinfo command: in the alter-table sql command the tablename inside the sql command should be equals to the filename of shapefile called externally.
In the ogrinfo command: in the update sql-command  there is the same relation name-table with name shapefile of the alter-table.



lunedì 2 giugno 2014

How find a word

I need to find a word "GAIA_VECTORS_LIST_FAST" but I don't know exactly in what file it can be.

So apply this command in a bash :

find ./  -type f -exec grep "GAIA_VECTORS_LIST_FAST" {} \; -print

et voila.

The response is:

  list = gaiaGetVectorLayersList( handle, NULL, NULL, GAIA_VECTORS_LIST_FAST );
./src/providers/spatialite/qgsspatialiteconnection.cpp

simple and fast.


sabato 24 maggio 2014

Retrieve the extents for every category

The need is to retrieve the BOX extent of every group of data.
The gropu are on field ANNO.

The solution is a group by, an Union and the Extent.
So:

select ANNO, ST_Union(GEOMETRY) from the_schema.the_table group by ANNO order by ANNO;

it work.

So I complete composing the string I need.

-----
select
    b.anno,
    'EXTENT ' || ST_XMin(b.extent)::integer - 100 || ' ' || ST_YMin(b.extent)::integer - 100 || ' ' || ST_XMax(b.extent)::integer + 100 || ' ' || ST_YMax(b.extent)::integer + 100 as extent
from
    (
        select anno, ST_Extent (geometry) as extent from the_schema.the_table group by anno
    ) as b
order by
    b.anno
;
-----

its all.

giovedì 22 maggio 2014

Enumerate all the domains from a set of datasets

The need is to retrieve the list of all the used domains from all fields of a set of dataset.

Load all DXF on a DB using the GUI in a Spatialite DB
I choose this options:

mixed-mode
epsg::2832
use the unlinked-ring
force all to 2D.


after load all DXF

I have 4 tables.

prefix_line_layer_2d
prefix_point_layer_2d
prefix_polyg_layer_2d
prefix_text_layer_2d

I run this command:

SELECT CreateMetaCatalogTables(1);

After the command, I have a table named "splite_metacatalog "
It has all the descriptions of the tables of DB. It say if are PKsingle or composite, if are NULL, and so on...

Now I can launch the
UpdateMetaCatalogStatistics(transaction TRUE|FALSE, table, column)

To have the list for all the tables (exclude PK) this is the call:

SELECT UpdateMetaCatalogStatistics(1, table_name, column_name)
FROM splite_metacatalog;

after i run this to see all column names

SELECT DISTINCT column_name from splite_metacatalog order by column_name;

 and I choose to accept only these columns: "layer" , "label", "rotation"

After I create the new table "freq" I run again the

drop  table if exists splite_metacatalog_statistics;
drop  table if exists splite_metacatalog;
SELECT CreateMetaCatalogTables(1);
SELECT UpdateMetaCatalogStatistics(1, 'freq', 'table_name', 'column_name');

now in the  "splite_metacatalog_statistics" I have the list or distinct values in the columns choosed and theis numerosity.

now I export in dbf:
. dumpdbf splite_metacatalog_statistics stats.dbf CP1252

et-voila.

martedì 20 maggio 2014

A font usable for cartography

One our need was to have a font open and free and narrow for our cartography.

We test some fonts.
 A good choice could be the Liberation Font, but unfortunately the  Narrow type has an ambigous not clear license.

So the choice was for the "Tuffy Font" that is a 100% public domain font.
The original home page is this:
http://tulrich.com/fonts/

Good.



domenica 18 maggio 2014

Find overlapped polygons

The goal is "how many polygons are overlapping other polygons in a dataset ?".

The solution is a simply spatial query:
The dataset is "dbcg_dgpv". 
"pk_uid" is the primary key.
"geometry" is the geometric field (polygon).

----
select
    a.pk_uid,
    b.pk_uid
from
    dbcg_dgpv as a inner join dbcg_dgpv as b on (a.pk_uid <> b.pk_uid)
where
    ST_contains(b.geometry,a.geometry) = 1
    AND
          a.ROWID IN (
        SELECT ROWID FROM SpatialIndex
        WHERE f_table_name = 'DB=main.dbcg_dgpv'
        AND search_frame =b.geometry
    );
----


The filter
..on (a.pk_uid <> b.pk_uid)
 is to avoid to compare a polygon with itself

lunedì 12 maggio 2014

TFW: Comma to Point

The TFW was build with the comma instead of point as decimal separator.
This is an issue for some GIS softwares so is need to resolve.

An example:
----
4,2336280797917594
8,7000389611034787e-005
-3,7967992801623495e-005
-4,2334471495687653
1687840,3554409891
4787158,6027319813
----

The solution is using the OSGEO4Wshell:

The sed command:
sed "/[0-9]\,/s/,/\./g" -i file.tfw < file.tfw

and a FOR cycle.

So:
suppose the tiff+tfw are all in this folder path
D:/temp/temp/prove

The for cycle is this:


for /R D:/temp/temp/prove %f IN (*.tfw) do sed "/[0-9]\,/s/,/\./g" -i %f < %f

Run and see that all the tfw are correctly with the point symbol.
like this:

---
4.2333743174393925
-2.3049048341267813e-005
-6.1198092636898077e-005
-4.2328667675685194
1577094.09993232
4940038.9227409437
---

ok, the problem is gone.

A.