Friday, June 29, 2012

Thailand MOT FDGS

หลังจากขอข้อมูลมาจาก http://gisportal.mot.go.th/Front/FAQ/FAQ.aspx

สร้าง FDGS.map
# ogrinfo -summary /var/mapfiles/wg00/road_WGS84.shp road_WGS84
# http://site:port/cgi-bin/mapserv?map=/path/road.map&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
# http://site:port/cgi-bin/mapserv?map=/path/road.map&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities

MAP
  IMAGETYPE      PNG
  EXTENT         384846.320137  635997.660954 1198331.211540 2258095.098300

  SIZE           1024 786
  SHAPEPATH      "/var/mapfiles"
  IMAGECOLOR     255 255 255
  CONFIG         "MS_ERRORFILE" "/var/tmp/mapserv.log"
  UNITS          meters
  FONTSET        "./fonts/fonts.list"   # this file contains only "loma Loma.ttf". And make sure that ./fonts/Loma.ttf exist.

  PROJECTION
    "proj=utm"
    "zone=47"
    "ellps=WGS84"
    "datum=WGS84"
    "units=m"
    "no_defs"
  END

  WEB
    METADATA
      "wms_title"           "roadmap"
      "wms_onlineresource"  "http://hostname/cgi-bin/mapserv?map=/path/to/this/FDGS.map&"
      "wms_srs"             "EPSG:4326"
      "wms_enable_request"  "!GetCapabilities"
    END
    IMAGEPATH "/var/www/name/tmp"
    IMAGEURL "/name/tmp"
  END

  LAYER
    NAME hydrology
    TYPE polygon
    DATA ./wg70/hydrology_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "hydrology"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "hydrology"
      STYLE
        COLOR        18 190 245
        OUTLINECOLOR 32 72 85
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END


  LAYER
    NAME admin
    TYPE polygon
    DATA ./wg80/admin_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "admin"
      "wms_format"  "image/png"
    END
    LABELITEM    "T_NAME_T"
    CLASS
      NAME "admin"
      STYLE
        #COLOR        0 0 0
        OUTLINECOLOR 60 20 231
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME Amphor
    TYPE polygon
    DATA ./wg90/Admin_Amphor_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "Amphor"
      "wms_format"  "image/png"
    END
    LABELITEM    "AMP_NAMT"
    CLASS
      NAME "Amphor"
      STYLE
        #COLOR        0 0 0
        OUTLINECOLOR 179 20 232
        WIDTH  2
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME Province
    TYPE polygon
    DATA ./wg100/Admin_Province_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "Province"
      "wms_format"  "image/png"
    END
    LABELITEM    "PROV_NAMT"
    CLASS
      NAME "Province"
      STYLE
        #COLOR        0 0 0
        OUTLINECOLOR 255 0 0
        WIDTH  3
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME police_area
    TYPE polygon
    DATA ./wg110/police_area_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "police_area"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "police_area"
      STYLE
        #COLOR        0 0 0
        OUTLINECOLOR 0 0 0
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END


  LAYER
    NAME road
    TYPE line
    DATA ./wg00/road_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "road"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "road"
      STYLE
        COLOR        150 150 150
        # OUTLINECOLOR 32 32 32
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620" # "UTF-8"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END # LABEL
    END
  END

  LAYER
    NAME expressway
    TYPE line
    DATA ./wg40/expressway_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "expressway"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "expressway"
      STYLE
        COLOR        139 105 20
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME transit
    TYPE line
    DATA ./wg50/transit_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "transit"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "transit"
      STYLE
        COLOR        255 255 0
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME railway
    TYPE line
    DATA ./wg60/railway_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "railway"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "transit"
      STYLE
        COLOR        66 195 30
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END


  LAYER
    NAME intersection
    TYPE point
    DATA ./wg10/intersection_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "intersection"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "intersection"
      STYLE
        SYMBOL  "cross"
        COLOR        255 0 0
        #WIDTH 5
        # OUTLINECOLOR 32 32 32
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END


  LAYER
    NAME station
    TYPE point
    DATA ./wg20/station_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "station"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "station"
      STYLE
        SYMBOL  "bus"
        COLOR        255 0 0
        #WIDTH 5
        # OUTLINECOLOR 32 32 32
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  LAYER
    NAME landmark
    TYPE point
    DATA ./wg30/landmark_WGS84
    DEBUG 4
    METADATA
      "wms_title"   "landmark"
      "wms_format"  "image/png"
    END
    LABELITEM    "NAME_T"
    CLASS
      NAME "landmark"
      STYLE
        SYMBOL  "star"
        COLOR        255 0 0
        #WIDTH 5
        # OUTLINECOLOR 32 32 32
      END
      LABEL
        FONT "loma"
        TYPE truetype
        ENCODING "TIS-620"
        SIZE 10
        POSITION LC
        PARTIALS FALSE
        COLOR  100 100 100
        OUTLINECOLOR 242 236 230
      END
    END
  END

  SYMBOL
    NAME "cross"
    TYPE vector
    POINTS
      2.0 0.0
      2.0 4.0
      -99 -99
      0.0 2.0
      4.0 2.0
    END
  END

  SYMBOL
    NAME "star"
    TYPE PIXMAP
    IMAGE "star.gif"
  END

  SYMBOL
    NAME "bus"
    TYPE PIXMAP
    IMAGE "bus.png"
  END

END 

FDGS.htm

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Language" content="th">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript" src="http://path/to/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
var map = null;
var markers = [];

var roadWMSMap;
var intersectionWMSMap;
var stationWMSMap;
var landmarkWMSMap;
var expresswayWMSMap;
var transitWMSMap;
var railwayWMSMap;
var hydrologyWMSMap;
var adminWMSMap;
var amphorWMSMap;
var provinceWMSMap;
var policeAreaWMSMap;

var baseURL =
      "http://hostname/cgi-bin/mapserv" +
      "?map=/var/mapfiles/FDGS.map&VERSION=1.3.0&REQUEST=GetMap&CRS=CRS:84"+
      "&FORMAT=image/png&WIDTH=256&HEIGHT=256&TRANSPARENT=TRUE";


function getBBox(tile, zoom) {
  var projection = window.map.getProjection();
  var zpow = Math.pow(2, zoom);
  var ul = new google.maps.Point(tile.x * 256.0 / zpow, (tile.y + 1) * 256.0 / zpow);
  var lr = new google.maps.Point((tile.x + 1) * 256.0 / zpow, (tile.y) * 256.0 / zpow);
  var ulw = projection.fromPointToLatLng(ul);
  var lrw = projection.fromPointToLatLng(lr);
  return ulw.lng() + "," + ulw.lat() + "," + lrw.lng() + "," + lrw.lat();
}

function renderWMS(){
  //http://code.google.com/apis/maps/documentation/javascript/reference.html#MVCArray
  //http://code.google.com/apis/maps/documentation/javascript/reference.html#Map
  //http://code.google.com/apis/maps/documentation/javascript/maptypes.html

  map.overlayMapTypes.clear();

  if(document.getElementById("road").checked){ map.overlayMapTypes.push(roadWMSMap); }
  if(document.getElementById("intersect").checked){ map.overlayMapTypes.push(intersectionWMSMap); }
  if(document.getElementById("admin").checked){ map.overlayMapTypes.push(adminWMSMap); }
  if(document.getElementById("province").checked){ map.overlayMapTypes.push(provinceWMSMap); }
  if(document.getElementById("amphor").checked){ map.overlayMapTypes.push(amphorWMSMap); }
  if(document.getElementById("express").checked){ map.overlayMapTypes.push(expresswayWMSMap); }
  if(document.getElementById("railway").checked){ map.overlayMapTypes.push(railwayWMSMap); }
  if(document.getElementById("transit").checked){ map.overlayMapTypes.push(transitWMSMap); }
  if(document.getElementById("hydro").checked){ map.overlayMapTypes.push(hydrologyWMSMap); }
  if(document.getElementById("station").checked){ map.overlayMapTypes.push(stationWMSMap); }
  if(document.getElementById("landmark").checked){ map.overlayMapTypes.push(landmarkWMSMap); }
  if(document.getElementById("police").checked){ map.overlayMapTypes.push(policeAreaWMSMap); }
}

(function ($) {
  $(document).ready(function() {
    initialize();

    var roadWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=road&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "Road Layer",
        tileSize: new google.maps.Size(256, 256)
    };
    roadWMSMap = new google.maps.ImageMapType(roadWMSOptions);

    var intersectionWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=intersection&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "intersection Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    intersectionWMSMap = new google.maps.ImageMapType(intersectionWMSOptions);

    var stationWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=station&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "station Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    stationWMSMap = new google.maps.ImageMapType(stationWMSOptions);

    var landmarkWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=landmark&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 16,
        name: "landmark Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    landmarkWMSMap = new google.maps.ImageMapType(landmarkWMSOptions);

    var expresswayWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=expressway&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "expressway Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    expresswayWMSMap = new google.maps.ImageMapType(expresswayWMSOptions);

    var transitWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=transit&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "transit Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    transitWMSMap = new google.maps.ImageMapType(transitWMSOptions);

    var railwayWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=railway&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "railway Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    railwayWMSMap = new google.maps.ImageMapType(railwayWMSOptions);

    var hydrologyWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=hydrology&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "hydrology Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    hydrologyWMSMap = new google.maps.ImageMapType(hydrologyWMSOptions);


    var adminWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=admin&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "admin Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    adminWMSMap = new google.maps.ImageMapType(adminWMSOptions);


    var amphorWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=Amphor&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "Amphor Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    amphorWMSMap = new google.maps.ImageMapType(amphorWMSOptions);


    var provinceWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=Province&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "Province Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    provinceWMSMap = new google.maps.ImageMapType(provinceWMSOptions);


    var policeAreaWMSOptions = {
        getTileUrl:
          function WMSGetTileUrl(tile, zoom) {
            return baseURL + "&LAYERS=police_area&STYLES=default&BBOX=" + getBBox(tile, zoom);
          },
        isPng: true,
        maxZoom: 17,
        minZoom: 1,
        name: "police_area Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    policeAreaWMSMap = new google.maps.ImageMapType(policeAreaWMSOptions);

  });


})(jQuery)


function initialize() {
  var controlOptions = { keepOpen : 1,position : 'BOTTOM_LEFT'};
  map = new google.maps.Map(document.getElementById('map_canvas'),
   {
    zoom: 10,
    center: new google.maps.LatLng(14.505156, 100.467224),
    mapTypeId: google.maps.MapTypeId.ROADMAP
   }
  );

  return false;
}

function clearMarkers() {
  for(i in markers){
    markers[i].setMap(null);
  }
  markers = [];
  //this.markers = new Array();
}

</script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>

</head>
<body >

<form action="" method="post" id="map-form" accept-charset="UTF-8">
  <table style="border:0px;">
    <tr>
      <td width="1000px" height="600px" style="padding:0px;vertical-align:top">
        <div id="map_canvas" style="width:100%; height:100%"/>
      </td>
      <td width="353px" style="padding:0px 0px 0px 15px;vertical-align:top">
        <div style="width:auto;height:500px;"id="edit-right-td" class="form-wrapper">
        <fieldset class="collapsible form-wrapper alpha60" id="edit-layer-set">
          <legend><span class="fieldset-legend">ข้อมูลที่แสดง</span></legend>
          <div class="fieldset-wrapper">
            <input type="checkbox" id="road" name="road" onclick="renderWMS();">เส้นทางคมนาคม<br/>
            <input type="checkbox" id="intersect" name="intersect" onclick="renderWMS();">จุดตัดถนน<br/>
            <input type="checkbox" id="admin" name="admin" onclick="renderWMS();">ตำบล<br/>
            <input type="checkbox" id="amphor" name="amphor" onclick="renderWMS();">อำเภอ<br/>
            <input type="checkbox" id="province" name="province" onclick="renderWMS();">จังหวัด<br/>
            <input type="checkbox" id="express" name="express" onclick="renderWMS();">เส้นทางด่วนทางยกระดับ<br/>
            <input type="checkbox" id="railway" name="railway" onclick="renderWMS();">เส้นทางรถไฟ<br/>
            <input type="checkbox" id="transit" name="transit" onclick="renderWMS();">เส้นทางรถไฟฟ้า<br/>
            <input type="checkbox" id="hydro" name="hydro" onclick="renderWMS();">เส้นทางคมนาคมทางน้ำ<br/>
            <input type="checkbox" id="station" name="station" onclick="renderWMS();">สถานีขนส่งสินค้าและผู้โดยสาร<br/>
            <input type="checkbox" id="landmark" name="landmark" onclick="renderWMS();">สถานที่สำคัญ<br/>
            <input type="checkbox" id="police" name="police" onclick="renderWMS();">ขอบเขตพื้นที่รับผิดชอบของตำรวจนครบาล<br/>
          </div>
         </fieldset>
        </div>
      </td>
    </tr>
  </table>
</form>
</body>
</html>








Thursday, May 17, 2012

My note of postgresql

oracle sysdate => now()

trunc(sysdate) => date_trunc('day', now());

rownum

SELECT 
    row_number() OVER () AS i, 
    e.col1, 
    e.col2, 
    ... FROM ... 



select ... from .. LIMIT 100;

sequence_name.nextval() => nextval('sequence_name')



java -jar /path/to/schemaSpy_5.0.0.jar -t pgsql -host MYHOSTNAME -db DBNAME -s SCHEMANAME -u USERNAME -p PASSWORD -o /path/to/output/directory -dp /path/to/postgresql-9.1-902.jdbc3.jar -charset utf-8


import
=====
copy {table_name}
from '/path/to/file.csv'
DELIMITER ',' CSV;



thanks
 http://stackoverflow.com/questions/3959692/rownum-in-postgresql

Wednesday, May 16, 2012

HTML ภาษาไทย


<head>
<meta http-equiv="Content-Language" content="th">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>

Wednesday, May 9, 2012

Cannot Connect PostgreSQL Remotely

could not connect to server: Connection refused Is the server running on host "......" (......) and accepting TCP/IP connections on port ....?

Solution
========
sudo vi /etc/postgresql/8.4/main/postgresql.conf

listen_addresses = '*'
port = 5432



After changing this file, you need to restart the server process to make the setting effective.

sudo service postgresql start



Note from pgadmin3
===================
For security reasons, PostgreSQL does not listen on all available IP addresses on the server machine initially. In order to access the server over the network, you need to enable listening on the address first.

For PostgreSQL servers starting with version 8.0, this is controlled using the "listen_addresses" parameter in the postgresql.conf file. Here, you can enter a list of IP addresses the server should listen on, or simply use '*' to listen on all available IP addresses.


Other Related Info
===================
sudo vi /etc/postgresql/8.4/main/pg_hba.conf

to allow connections from any remote address.
host all all 0.0.0.0/0 md5

psql -U {username} -h {hostname} -D {database_name}


thanks http://kb.mediatemple.net/questions/1237/How+do+I+enable+remote+access+to+my+PostgreSQL+server%3F#dv

Thursday, April 26, 2012

Linux Troubleshooting: Boot

boot เครื่องใหม่และกำหนด kernel parameter เพิ่ม "init=/bin/bash" เข้าไป

วิธีนี้จะทำให้หลังจาก boot kernel เสร็จมันจะ execute /bin/bash แทนที่จะเป็น init
และมันจะไม่ execute rc script ตัวใหนเลย แต่เราจะสามารถใช้ shell บน terminal ได้

ตอนที่ boot โดยใช้ init=/bin/bash (หรือ init=/bin/sh )
root file system จะ mount เป็น read only เราจะไม่สามารถแก้ไขอะไรบน file system ได้

ลองใช้ คำสั่ง "ls > /tmp/x" ดูนะครับ จะเห็นว่ามันไม่สามารถสร้างไฟล์ได้

จะต้อง remount มันให้เป็นแบบ read/write ก่อน โดยการใช้คำสั่ง

# mount -o remount,rw /

หลังจากนั้น ลองใช้คำสั่ง "ls > /tmp/x" จะเห็นว่ามันสามารถสร้างไฟล์ /tmp/x ขึ้นมาได้

หลังจาก filesystem mount เป็นแบบ read/write แล้ว เราก็จะสามารถแก้ไข ลำดับ หรือ script
ที่ init จะ execute ได้

สมมตว่า สงสัยว่า rc script ที่ชื่อ /etc/rcS.d/S06hwclockfirst.sh เป็นตัวทำให้เครื่องหยุดทำงาน
ก็ลอง rename มันชื่ออื่นก่อน เช่น

# cd /etc/rcS.d
# mv S06hwclockfirst.sh _S06hwclockfirst.sh

เปลี่ยนชื่อไฟล์ ให้ตัวอักษรตัวแรก ไม่เป็น 'S' -- สำหรับ Start หรือ 'K' -- Kill สำหรับ
stop script จะทำให้ในขั้นตอนของการ boot up หรือ shutdown เครื่อง มันไม่ execute script
เหล่านั้น

rename โดยให้ยังคงชื่อเดิมเอาไว้เพราะในครั้งต่อไป เราอาจจะต้อง rename มันกลับเป็นชื่อเดิมอีกครั้ง
ผมใช้วิธีการเพิ่ม '_' เข้าไปหน้าชื่อ

หลังจากนี้ ก็ต้อง reboot เครื่อง แต่จะใช้คำสั่ง reboot หรือ shutdown ไม่ได้เพราะเราไม่ได้ boot เครื่อง
โดยใช้ init ตามปกติ ... ผมไม่แน่ใจว่ามีคำสั่งใหนบ้างที่จะ force reset มันได้ โดยไม่ไป execute script
ผมใช้วิธีการกดปุ่ม reset หรือ ปุ่ม power เลย

แต่ก่อนที่จะทำอย่างนั้น เพื่อความสบายใจว่า เรา mount filesystem เป็นแบบ read/write อยู่ อาจจะทำให้
file เสียหายถ้ากดปุ่ม reset เลย ก็ใช้วิธีการ remount ให้มันกลับเป็น แบบ read only ก่อน

# sync
# mount -o remount,ro /

แล้วค่อยกดปุ่ม reset ครับ

คราวนี้ให้มัน boot แบบ verbose ตามปกติแล้วดูว่ามันไปหยุดที่ใหน
แล้ว reboot เครื่องใหม่ โดยใช้ init=/bin/bash อีกครั้ง
เข้าไปดูว่ามันหยุดที่ script ตัวใหน
ใช้ คำสั่ง grep หรือ more สำหรับ ดูข้อมูลในไฟล์ได้ ว่า script ตัวใหน
เป็นตัวพิมพ์ string ล่าสุดออกมาก่อนที่มันจะหยุดไป
หลีกเลี่ยงการใช้ editor แบบ nano/vi/... นะครับ :)

ที่มา http://debianclub.org/node/736

Tuesday, April 17, 2012

Fix Can't locate perl Install.pm debian



$ perl Makefile.PL
Can't locate inc/Module/Install.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at Makefile.PL line 5.
BEGIN failed--compilation aborted at Makefile.PL line 5.

$ sudo apt-get install libmodule-install-perl

thanks
http://www.bioperl.org/wiki/Tutorial:Installing_Perl_modules#Debian

Monday, March 19, 2012

D7 alter login form set default username password



function MYMODULE_form_user_login_block_alter(&$form, &$form_state) {
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'MYMODULE') . '/js/login_form.js',
  );
}



login_form.js
==========
(function ($) {
  $(document).ready(function() {
    var name = document.getElementById("edit-name");
    name.value = "demo";

    var pass = document.getElementById("edit-pass");
    pass.value = "password";
  });
})(jQuery)





Sunday, March 18, 2012

purge drupal taxonomy term data


  // drush php-script purge_taxonomy_term.php

  $vocab_vid = taxonomy_vocabulary_machine_name_load("vocab_name")->vid;

  $terms = taxonomy_get_tree($vocab_vid);
  foreach($terms as $term) {
    //$term = taxonomy_term_load($term->tid);
    taxonomy_term_delete($term->tid);
  }

Monday, February 20, 2012

Upload & Process uploaded file drupal

function test_import($form, &$form_state) {
  $form['csv_file'] = array(
      '#title'          => t('Upload a csv file'),
      '#type'         => 'file',
      '#description'  => t('Must be smaller than 5MB in size.'),
     );
  $form[] = array(
      '#type'  => 'submit',
      '#value'  => 'Process',
    );

  return $form;
}

function test_import_validate($form, &$form_state) {
  $validators = array(
     'file_validate_extensions' => array( 'csv' ),
     'file_validate_size' => array(variable_get('btn_file_size', '5') * 1024),
  );
  // the others validators are http://api.drupal.org/api/drupal/includes!file.inc/group/file/7

  $destination = FALSE;
  // $destination = 'public://testimport/';

  $fileObj = file_save_upload('csv_file', $validators, $destination);
  if (!$fileObj) {
    form_set_error('csv_file', t('Oh Unable to access file or file is missing.'));
  }

}

function test_import_submit($form, &$form_state) {

  $fileObj = file_save_upload( 'csv_file');
  if (!$fileObj) {
    form_set_error('csv_file', t('Unable to access file or file is missing.'));
  }

  ini_set('auto_detect_line_endings', true);
  $file = fopen( drupal_realpath($fileObj->uri), 'r');

  while(!feof($file)) {
    $line = fgets($file);
    $data = explode("\t", $line);
    dpm($data[0]);
  }

  fclose($file);

}


related :
http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#managed_file
http://api.drupal.org/api/examples/image_example%21image_example.pages.inc/7

Friday, February 10, 2012

clickable table row on drupal's tableselect

YOUR.inc
=========
function a_form($form, &$formstate){
  drupal_add_js(drupal_get_path('module', 'YOUR-MODULE-NAME') . '/js/selectable-row.js', 'file');
 
  $header = array
  (
    'name' => 'name',
    'surname' => 'surname',
    'action' => 'action',
  );

  $options['1'] = array(
    'name' => 'name1',
    'surname' => 'surname1',
    'action' => '<a href="http://...">link</a>',
  );

  $options['2'] = array(
    'name' => 'name2',
    'surname' => 'surname2',
    'action' => '<a href="http://...">link</a>',
  );

  $form['tableselect_field'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#multiple' => FALSE,
    '#ajax' => array(
      'callback' => 'an_example_callback',
      'wrapper' => 'a-div',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#attributes' => array('class' => array('selectable-row')),
  );

  $form['ajax_div'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="a-div">',
    '#markup' => date('y-m-d h:i:s'),
    '#suffix' => '</div>',
  );

  return $form;
}

function an_example_callback($form, $form_state) {
  return $form['ajax_div'];
}


selectable-row.js
===================
(function ($) {

  $(document).ready(function() {

    var selectableRow = $("table.selectable-row tbody").children();

    selectableRow.hover(function () {
      $(this).addClass("highlight");
    }, function () {
      $(this).removeClass("highlight");
    });

    selectableRow.click(function() {
      $(this).siblings().removeClass("selected-row");
      $(this).addClass("selected-row");
      $(this).find(":radio").attr("checked","checked");
      $(this).find(":radio").change();
    });

    // snippet with some modification from
    // http://thephuse.com/design-and-usability/clickable-table-rows-with-jquery/
    selectableRow.find('a').hover( function() {
        $(this).parents('tr').unbind('click');
    }, function() {
        $(this).parents('tr').click( function() {
          $(this).siblings().removeClass("selected-row");
          $(this).addClass("selected-row");
          $(this).find(":radio").attr("checked","checked");
          $(this).find(":radio").change();
        });
    });

  });
})(jQuery)


YOUR.css
=========
tr.highlight {
  background-color: #0000EE;
  color: #FFF;
}

tr.selected-row {
  background-color: #00AAEE;
  color: #FE0;
}

Wednesday, February 8, 2012

Postgresql File System Level Backup Debian

In order to perform postgresql backup on debain, there are some modification on
http://www.postgresql.org/docs/9.1/interactive/backup-file.html


sudo service apache2 stop
sudo service postgresql stop

#%y%m%d_%H-%M
sudo tar -cvpjf /a/path/postgres-`date +%y%m%d`.tar.bz2 -C /var/lib/postgresql/9.1/main .

# Prevent parent directories from being tarred
#   -C, --directory DIR
#           change to directory DIR

# -p, --preserve-permissions, --same-permissions
#           extract information about file permissions (default for superuser)


sudo service postgresql start
sudo service apache2 start

# when extract
#sudo tar -xvjf postgres-xxxxxx.tar.bz2

Friday, January 20, 2012

run noip duc automatically when debian is booted

sudo cp noip-duc-linux/noip-2.1.9-1/debian.noip2.sh /etc/init.d/noip2
sudo chown root:root /etc/init.d/noip2
sudo chmod 755 /etc/init.d/noip2

according to http://wiki.debian.org/LSBInitScripts/DependencyBasedBoot
instead of
update-rc.d noip2 defaults

add "INIT INFO" to /etc/init.d/noip2

### BEGIN INIT INFO
# Provides: noip2
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop directadmin
# Description: DirectAdmin
### END INIT INFO

then
sudo insserv noip2
(with out the INIT INFO, the error "insserv: warning: script 'X' missing LSB tags and overrides"  will be shown.)

the whole file will be

#! /bin/sh
# /etc/init.d/noip2.sh

# Supplied by no-ip.com
# Modified for Debian GNU/Linux by Eivind L. Rygge
# corrected 1-17-2004 by Alex Docauer

# . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc

### BEGIN INIT INFO
# Provides: noip2
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop directadmin
# Description: DirectAdmin
### END INIT INFO

exec >> /tmp/debug-my-script.txt 2>&1
echo $1


DAEMON=/usr/local/bin/noip2
NAME=noip2

test -x $DAEMON || exit 0

case "$1" in
    start)
        echo -n "Starting dynamic address update: "
        start-stop-daemon --start --exec $DAEMON
        echo "noip2."
        ;;
    stop)
        echo -n "Shutting down dynamic address update:"
        start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON
        echo "noip2."
        ;;
    restart)
        echo -n "Restarting dynamic address update: "
        start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON
        start-stop-daemon --start --exec $DAEMON
        echo "noip2."
        ;;
   status)
        echo "noip2."
            ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0
when disable noip2 type
sudo insserv -r noip2